使用 Aspose.HTML for Java 将 EPUB 转换为 PDF
EPUB 是一种广泛用于电子书的格式,但它并不总是与所有设备或软件应用程序兼容。将 EPUB 转换为 PDF 有助于创建可在不同设备和平台上轻松阅读的文档。PDF 针对打印进行了优化,因此非常适合创建文档的实体副本。您可以为 PDF 文件配置安全设置,以限制访问或编辑。
本文将介绍如何使用 Aspose.HTML for Java 库将 EPUB 转换为 PDF,以及如何使用 PdfSaveOptions。
通过几行 Java 代码将 EPUB 转换为 PDF
Converter 类的静态方法主要用于将 EPUB 文件转换为各种格式的最简单方法。只需几行代码,您就可以在 Java 应用程序中将 EPUB 转换为 PDF!
1// Convert EPUB to PDF using Java
2
3// Open an existing EPUB file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
5
6// Invoke the convertEPUB() method to convert EPUB to PDF
7Converter.convertEPUB(fileInputStream, new PdfSaveOptions(), "convert-by-two-lines.pdf");用 Java 将 EPUB 转换为 PDF
您应该遵循几个步骤:
- 打开现有的 EPUB 文件。在示例中,代码初始化了一个
FileInputStream,以字节流的形式从指定路径的文件系统中读取 EPUB 文件的内容。 - 创建 PdfSaveOptions 的实例。使用空的 PdfSaveOptions() 构造函数转换默认保存选项。
- 使用
Converter 类的
convertEPUB(stream, options, savePath)方法将 EPUB 保存为 PDF 文件。您需要将 EPUB 文件流、PdfSaveOptions 和输出文件路径作为参数传递给convertEPUB()方法。
下面的 Java 代码片段显示了如何使用 Aspose.HTML for Java 将 EPUB 转换为 PDF:
1// Convert EPUB to PDF in Java
2
3// Open an existing EPUB file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
5
6// Create an instance of the PdfSaveOptions class
7PdfSaveOptions options = new PdfSaveOptions();
8
9// Call the convertEPUB() method to convert EPUB to PDF
10Converter.convertEPUB(fileInputStream, options, "output-epub-to-pdf.pdf");您可以从 GitHub 下载完整的示例和数据文件。
保存选项 – PdfSaveOptions 类
Aspose.HTML 可使用 PdfSaveOptions以编程方式将 EPUB 转换为 PDF,并可完全控制各种转换选项。该功能允许用户通过设置 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 将 EPUB 转换为 PDF
通过设置
PdfSaveOptions 类的不同选项,你可以控制输出 PDF 的页面大小、分辨率、背景颜色和压缩设置。下面的示例展示了如何使用 PdfSaveOptions创建具有自定义页面大小和背景颜色的 PDF 文件:
- 打开现有 EPUB 文件
- 创建一个新的
PdfSaveOptions 对象,并指定所需的保存选项。使用
setPageSetup()和setBackgroundColor()方法设置页面大小和背景颜色。 - 调用
convertEPUB(stream, options, savePath)方法将 EPUB 保存为 PDF 文件。您需要将 EPUB 文件流、PdfSaveOptions 和输出文件路径作为参数传递给 convertEPUB() 方法。
1// Convert EPUB to PDF in Java with custom settings
2
3// Open an existing EPUB file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
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(1000), Length.fromPixels(1000)));
11
12// Call the convertEPUB() method to convert EPUB to PDF
13Converter.convertEPUB(fileInputStream, options, "input-options.pdf");结论
使用 Aspose.HTML library for Java 将 EPUB 转换为 PDF 是创建普遍兼容的印刷就绪文档的一种简单而有效的方法。使用 PdfSaveOptions类,开发人员可通过页面大小、背景颜色、分辨率和加密等选项自定义输出,确保最终 PDF 文件满足特定要求。所考虑的 Java 示例演示了如何在 Java 应用程序中实现转换功能。
检查我们的在线 EPUB到PDF转换器EPUB到PDF转换的质量。上传,转换您的文件,并在几秒钟内得到的结果。现在就免费试用我们强大的EPUB到PDF Converter!
