用 Java 将 MHTML 转换为 PDF
Aspose.HTML for Java 的主要功能之一就是能够轻松可靠地将 MHTML 文档转换为其他格式。PDF 有许多优点,MHTML 到 PDF 的转换可用于共享、存档或打印网页。在本文中,您将了解到MHTML到PDF转换场景的相关信息,并学习如何使用 PdfSaveOptions。
用几行代码将 MHTML 转换为 PDF
Converter 类的静态方法主要用于将 MHTML 文件转换为其他格式的最简单方法。只需几行代码,您就可以将 MHTML 转换为 PDF!
1// Convert MHTML to PDF using Java
2
3// Open an existing MHTML file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
5
6// Invoke the convertMHTML() method to convert MHTML to PDF
7Converter.convertMHTML(fileInputStream, new PdfSaveOptions(), "convert-by-two-lines.pdf");用 Java 将 MHTML 转换为 PDF
让我们一步步来了解一下简单的 MHTML 转换为 PDF 的情况:
- 加载 MHTML 文件您可以从文件、流或 URL 加载 MHTML。在示例中,我们使用
FileInputStream类以字节流的形式读取 MHTML 文件。 - 创建一个新的 PdfSaveOptions 对象。
- 使用
Converter 类的
сonvertMHTML(stream, options, savePath)方法将 MHTML 文档保存为 PDF 文件。该方法将stream、options和savePath作为参数,并执行转换。
1// Convert MHTML to PDF using Java
2
3// Open an existing MHTML file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
5
6// Create an instance of the PdfSaveOptions class
7PdfSaveOptions options = new PdfSaveOptions();
8
9// Call the convertMHTML() method to convert MHTML to PDF
10Converter.convertMHTML(fileInputStream, options, "sample-output.pdf");您可以从 GitHub 下载完整的示例和数据文件。
保存选项 – PdfSaveOptions 类
Aspose.HTML for Java 允许使用默认或自定义保存选项将 MHTML 转换为 PDF。通过 PdfSaveOptions,您可以自定义渲染过程。您可以指定 page size、 margins、 file permissions、 media type等。
| Metod | Description |
|---|---|
| setJpegQuality(value) | Specifies the quality of JPEG compression for images. The default value is 95. |
| 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. |
| setEncryption | This method gets or sets encryption details. If it is not set, then no encryption will be performed. |
有关如何使用 PdfSaveOptions自定义转换过程的详细信息,请参阅 Fine-Tuning Converters 一文。
使用 PdfSaveOptions 将 MHTML 转换为 PDF
使用 Aspose.HTML for Java,您可以通过编程转换文件,并完全控制各种转换参数。要使用指定的 PdfSaveOptions将 MHTML 转换为 PDF,您需要遵循以下几个步骤:
- 加载 MHTML 文件。
- 创建一个新的
PdfSaveOptions 对象,并指定所需的属性。在下面的示例中,我们将为生成的 PDF 文档应用自定义页面大小和背景颜色:
- Use the
setPageSetup()method to specify the page size for the output PDF document. - Use the
setBackgroundColor()method to set the color that fills the background.
- Use the
- 调用
Converter类的 convertMHTML(stream,options,savePath) 方法。
1// Convert MHTML to PDF in Java with custom settings
2
3// Open an existing MHTML file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
5
6// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
7PdfSaveOptions options = new PdfSaveOptions();
8options.setBackgroundColor(Color.getAliceBlue());
9options.getPageSetup().setAnyPage(new Page());
10options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(3000), Length.fromPixels(1000)));
11
12// Call the convertMHTML() method to convert MHTML to PDF
13Converter.convertMHTML(fileInputStream, options, "sample-options.pdf");结论
Aspose.HTML for Java 为用户提供了将 MHTML 转换为 PDF 的多功能、强大的 API。转换过程包括加载一个 MHTML 文件,创建一个带有默认或自定义选项的 PdfSaveOptions 对象,并使用转换器类的 convertMHTML() 方法。通过这些简单的步骤,您就可以创建准确、高质量的 PDF 文档,满足您的需求。
Aspose.HTML 提供免费的在线 MHTML 到 PDF 转换器,可将 MHTML 转换为高质量的 PDF,简单快捷。只需上传、转换您的文件,几秒钟就能得到结果!
