使用 Java 将 HTML 转换为 XPS

XPS 是微软公司创建的一种文档格式,具有多种优点,包括数字签名等高级安全功能,可增加文档保护。XPS 文件提供访问权限管理,并能生成高质量的可打印文档。因此,HTML 到 XPS 的转换可以为管理和共享文档提供更大的灵活性和功能性,使其在某些情况下成为有价值的工具。

本文将介绍如何使用 Aspose.HTML for Java 库将 HTML 转换为 XPS 以及如何应用 XpsSaveOptions

通过一行 Java 代码将 HTML 转换为 XPS

Converter 类的静态方法主要用于将 HTML 代码转换为各种格式的最简单方法。只需一行代码,您就可以在 Java 应用程序中将 HTML 转换为 XPS

1// Convert HTML to XPS using Java
2
3// Invoke the convertHTML() method to convert HTML to XPS
4Converter.convertHTML("<h1>Convert HTML to XPS!</h1>", ".", new XpsSaveOptions(), "convert-with-single-line.xps");

在示例中,我们使用了 Converter 类的 convertHTML(content, baseUri, options, outputPath) 方法,该方法需要四个参数:包含要转换的 HTML 代码的字符串、输入 HTML 文件的基本文件夹、XpsSaveOptions 类的实例以及保存转换后文件的输出文件路径。

用 Java 将 HTML 转换为 XPS

使用 convertHTML()方法将文件转换为另一种格式是一系列操作,其中包括文件加载和保存:

  1. 使用 HTMLDocument 类加载 HTML 文件。
  2. 创建一个新的 XpsSaveOptions 对象。使用空的 XpsSaveOptions() 构造函数转换默认保存选项。
  3. 使用 Converter 类的 convertHTML() 方法将 HTML 保存为 XPS 文件。您需要向 convertHTML() 方法传递 HTMLDocument、XpsSaveOptions 和输出文件路径,以便将 HTML 转换为 XPS。
 1// Convert HTML to XPS using Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "<span>Hello, World!!</span>";
 5try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
 6    fileWriter.write(code);
 7}
 8
 9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("document.html");
11
12// Initialize XpsSaveOptions
13XpsSaveOptions options = new XpsSaveOptions();
14
15// Convert HTML to XPS
16Converter.convertHTML(document, options, "document-output.xps");

您可以从 GitHub 下载完整的示例和数据文件。

保存选项 – XpsSaveOptions 类

有了 XpsSaveOptions,你就可以个性化 XPS 文件的渲染过程。这包括自定义页面大小、页边距、背景颜色、CSS 媒体类型等元素。

MetodDescription
getCss()Gets a CssOptions object which is used for configuration of CSS properties processing.
setBackgroundColor(value)Sets the color that will fill the background of every page. By default, this property is Transparent.
setPageSetup(value)This method sets a page setup object and uses it for configuration output page-set.
setHorizontalResolution(value)Sets horizontal resolution for internal images, in pixels per inch. By default this property is 300 dpi.
setVerticalResolution(value)Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.

要了解有关 XpsSaveOptions 的更多信息,请阅读 Fine-Tuning Converters 一文。

使用 XpsSaveOptions 将 HTML 转换为 XPS

XpsSaveOptions 类提供了大量属性,可让您全面控制各种参数,并改进 HTML 转换为 XPS 格式的过程。下面的示例展示了如何使用 XpsSaveOptions 创建具有自定义页面大小和背景颜色的 XPS 文件:

  1. 使用 HTMLDocument 类的 HTMLDocument() 构造函数之一加载 HTML 文件。
  2. 创建一个新的 XpsSaveOptions 对象。
  3. 使用转换器类的 convertHTML() 方法将 HTML 保存为 XPS 文件。您必须向该方法传递参数–documentoptions 和输出文件路径 savePath 以执行转换。

下面的示例显示了如何使用 XpsSaveOptions 并创建具有自定义页面大小和背景颜色的 XPS 文件:

 1// Convert HTML to XPS with custom settings using Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "<h1>  XpsSaveOptions Class</h1>\r\n" +
 5        "<p>Using XpsSaveOptions Class, you can programmatically " +
 6        "apply a wide range of conversion parameters " +
 7        "such as BackgroundColor, PageSetup, etc.</p>";
 8
 9FileHelper.writeAllText("save-options.html", code);
10
11// Initialize an HTML document from the html file
12HTMLDocument document = new HTMLDocument("save-options.html");
13
14// Set up the page-size, margins and change the background color to AntiqueWhite
15XpsSaveOptions options = new XpsSaveOptions();
16options.setBackgroundColor(Color.getAntiqueWhite());
17options.getPageSetup().setAnyPage(new Page(new Size(Length.fromInches(4.9f), Length.fromInches(3.5f)), new Margin(30, 20, 10, 10)));
18
19// Convert HTML to XPS
20Converter.convertHTML(document, options, "save-options-output.xps");

您可以从 GitHub 下载完整的示例和数据文件。

使用我们的在线 HTML 到 XPS 转换器 检查 HTML 到 XPS 的转换质量。上传、转换您的文件并在几秒钟内获得结果。现在就免费试用我们强大的 HTML 到 XPS 转换器吧!

文本 “HTML 至 XPS 转换器”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.