Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
XPS 是微软公司开发的一种文档存储和查看格式。它具有一系列支持安全功能的优点,如数字签名可提供更高的文档安全性等。要建立有限的文档编辑或复制访问权限,通常需要将 HTML 转换为 XPS。XPS 文件格式提供访问权限管理,并提供高质量的可打印文档。XPS 文件可用于共享文档,使用 XPS 查看器时,您可以确保您在页面上看到的内容与其他人看到的内容相同。
使用 Converter.ConvertHTML() 方法是将 HTML 代码转换为各种格式的最常用方法。使用 Aspose.HTML for .NET,您可以通过编程将 HTML 转换为 XPS 格式,并完全控制各种转换参数。本文将介绍如何使用 Converter 类的 ConvertHTML() 方法将 HTML 转换为 XPS,以及如何应用 XpsSaveOptions 和 ICreateStreamProvider 参数。
您可以检查 Aspose.HTML API 功能并实时转换 HTML。请从本地文件系统加载 HTML,选择输出格式并运行示例。在示例中,默认设置了保存选项。您将立即以单独文件的形式收到结果。
如果您想以编程方式将 HTML 转换为 XPS,请参阅以下 C# 代码示例。
Converter 类的静态方法主要用作将 HTML 代码转换为各种格式的最简单方法。只需一行代码,您就可以在 C# 应用程序中将 HTML 转换为 XPS!
1// Convert HTML to XPS using C#
2
3// Invoke the ConvertHTML() method to convert the HTML code to XPS
4Converter.ConvertHTML(@"<h1>Convert HTML to XPS!</h1>", ".", new XpsSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.xps"));使用 ConvertHTML() 方法将文件转换为另一种格式是一系列操作,其中包括文件加载和保存:
请看下面的 C# 代码片段,它显示了使用 Aspose.HTML for .NET 将 HTML 转换为 XPS 的过程。
1// Convert HTML to XPS in C#
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "canvas.html");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "canvas-output.xps");
8
9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Initialize XpsSaveOptions
13XpsSaveOptions options = new XpsSaveOptions();
14
15// Convert HTML to XPS
16Converter.ConvertHTML(document, options, savePath);Aspose.HTML 允许使用默认或自定义保存选项将 HTML 转换为 XPS。使用 XpsSaveOptions 可以自定义渲染过程。您可以指定页面大小、页边距、Css 等。
| Property | Description |
|---|---|
| PageSetup | This property gets a page setup object and uses it for configuration output page-set. |
| Css | Gets a CssOptions object which is used for configuration of CSS properties processing. |
| BackgroundColor | This property sets the color that will fill the background of every page. By default, this property is Transparent. |
| HorizontalResolution | Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
| VerticalResolution | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
要了解有关 XpsSaveOptions 的更多信息,请阅读 微调转换器 一文。
要使用指定的 XpsSaveOptions 将 HTML 转换为 XPS,应遵循以下几个步骤:
下面的示例显示了如何使用 XpsSaveOptions,并创建具有自定义页面大小和背景颜色的 XPS 文件:
1// Convert HTML to XPS with custom settings using C#
2
3string documentPath = Path.Combine(OutputDir, "save-options.html");
4string savePath = Path.Combine(OutputDir, "save-options-output.xps");
5
6// Prepare HTML code and save it to a file
7string code = "<h1> XpsSaveOptions Class</h1>\r\n" +
8 "<p>Using XpsSaveOptions Class, you can programmatically apply a wide range of conversion parameters such as BackgroundColor, PageSetup, etc.</p>\r\n";
9
10File.WriteAllText(documentPath, code);
11
12// Initialize an HTML Document from the html file
13using HTMLDocument document = new HTMLDocument(documentPath);
14
15// Set up the page-size, margins and change the background color to AntiqueWhite
16XpsSaveOptions options = new XpsSaveOptions()
17{
18 BackgroundColor = System.Drawing.Color.AntiqueWhite
19};
20options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(4.9f), Length.FromInches(3.5f)), new Margin(30, 20, 10, 10));
21
22// Convert HTML to XPS
23Converter.ConvertHTML(document, options, savePath);XpsSaveOptions() 构造函数初始化 XpsSaveOptions 类的实例,并将其传递给 ConvertHTML() 方法。ConvertHTML() 方法接收 document, options, 输出文件路径 savePath 并执行转换操作。XpsSaveOptions 类提供了大量属性,可让您全面控制各种参数,并改进 HTML 转换为 XPS 格式的过程。
在上述示例中,我们使用
如果需要将文件保存在远程存储器(如云、数据库等)中,可以实现 ICreateStreamProvider接口来手动控制文件创建过程。该接口被设计为一个回调对象,用于在文档/页面开始时创建一个流(取决于输出格式),并在渲染文档/页面后释放早期创建的流。
下面的示例展示了如何在应用程序中实现和使用自己的MemoryStreamProvider:
1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
2
3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
4{
5 // List of MemoryStream objects created during the document rendering
6 public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
7
8 public Stream GetStream(string name, string extension)
9 {
10 // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11 MemoryStream result = new MemoryStream();
12 Streams.Add(result);
13 return result;
14 }
15
16 public Stream GetStream(string name, string extension, int page)
17 {
18 // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19 MemoryStream result = new MemoryStream();
20 Streams.Add(result);
21 return result;
22 }
23
24 public void ReleaseStream(Stream stream)
25 {
26 // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27 }
28
29 public void Dispose()
30 {
31 // Releasing resources
32 foreach (MemoryStream stream in Streams)
33 stream.Dispose();
34 }
35} 1// Convert HTML to XPS in C# using memory stream
2
3// Create an instance of MemoryStreamProvider
4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
5
6// Initialize an HTML document
7using HTMLDocument document = new HTMLDocument(@"<h1>Convert HTML to XPS File Format!</h1>", ".");
8
9// Convert HTML to XPS using MemoryStreamProvider
10Converter.ConvertHTML(document, new XpsSaveOptions(), streamProvider);
11
12// Get access to the memory stream that contains the result data
13MemoryStream memory = streamProvider.Streams.First();
14memory.Seek(0, SeekOrigin.Begin);
15
16// Flush the result data to the output file
17using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.xps")))
18{
19 memory.CopyTo(fs);
20}您可以从 GitHub 下载完整的示例和数据文件。
Aspose.HTML 提供免费的在线 HTML to XPS Converter,可将 HTML 高质量、方便快捷地转换为 XPS。只需上传、转换您的文件,几秒钟就能得到结果!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.