在 Java 中将 EPUB 转换为 PNG

EPUB 是最广泛支持的电子书格式。当你想在PowerPoint演示文稿中加入EPUB文件或通过电子邮件发送时,将EPUB转换为PNG会很有帮助。或者,例如,你想与没有安装 EPUB 阅读器的人共享 EPUB 文件。

Aspose.HTML for Java库提供多种EPUB转换图像的功能,如JPGPNGBMPTIFFGIF。将EPUB转换为任何支持的图像格式都需要遵循以下必要步骤:

唯一的区别是使用 ImageSaveOptions 类指定输出图像格式。ImageSaveOptions(format) 构造函数用指定的格式初始化选项对象。您可以将图像格式设置为 JPG、PNG、BMP、GIF 或 TIFF。默认的 ImageFormat 为 PNG。

本文将介绍如何使用Aspose.HTML for Java将EPUB转换为PNG,以及如何应用 ImageSaveOptions。您可以轻松使用Java示例将EPUB转换为PNG,这里详细介绍了如何将 EPUB转换为JPG、EPUB转换为BMP、EPUB转换为GIF和EPUB转换为TIFF。只需将 ImageFormat设置为 ImageSaveOptions即可!

通过几行 Java 代码将 EPUB 转换为 PNG

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

1// Convert EPUB to PNG using Java
2
3// Open an existing EPUB file for reading
4FileInputStream inputStream = new FileInputStream("input.epub");
5
6// Convert EPUB to PNG
7Converter.convertEPUB(inputStream, new ImageSaveOptions(), "convert-with-single-line.png");

将 EPUB 转换为 PNG

让我们通过一个简单的EPUB到PNG转换方案的逐步说明:

  1. 打开现有的 EPUB 文件。在示例中,代码初始化了 FileInputStream,以字节流的形式读取 EPUB 文件的内容。
  2. 创建 ImageSaveOptions 的实例。默认图像格式为 ImageFormat.Png
  3. 使用 Converter 类的 convertEPUB(stream, options, savePath) 方法将 EPUB 保存为 PNG 图像。该方法将 streamoptionssavePath 作为参数,并执行转换。

下面的 Java 代码片段展示了如何使用 Aspose.HTML 将 EPUB 转换为 PNG:

 1// Convert EPUB to PNG 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 ImageSaveOptions class
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
 8
 9// Call the сonvertEPUB() method to convert EPUB to PNG
10Converter.convertEPUB(fileInputStream, options, "input-output.png");

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

保存选项 - ImageSaveOptions 类

ImageSaveOptions 允许用户通过设置 image format、页面大小、页边距、 compression levelmedia type 和其他参数来定制渲染过程。

MethodDescription
setCompression(value)Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW.
getCssGets a CssOptions object which is used for configuration of CSS properties processing.
setFormat(value)Sets ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default this property is ImageFormat.Png.
setBackgroundColor(value)Sets Color which will fill background of every page. Default value is Color.Transparent(Color.getTransparent()).
setPageSetup(value)Gets a page setup object is used for configuration output page-set.
setHorizontalResolution(value)Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi.
setVerticalResolution(value)Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
setSmoothingMode(value)Sets the rendering quality for this image.
getText()Gets a TextOptions object which is used for configuration of text rendering.

通过指定图像保存选项将 EPUB 转换为 PNG

Aspose.HTML for Java 允许使用默认或自定义保存选项将 EPUB 转换为 PNG。下面的 Java 示例展示了如何使用 ImageSaveOptions(图像保存选项)并创建具有自定义渲染质量、分辨率和背景颜色的 PNG 文件:

  1. 打开现有的 EPUB 文件。初始化 FileInputStream,以字节流的形式读取 EPUB 文件的内容。
  2. 使用 PNG ImageFormat 创建一个新的 ImageSaveOptions 对象,并指定所需的保存选项:
    • Use the setSmoothingMode() method to set the quality of the image smoothing to high.
    • Use the setHorizontalResolution() and setVerticalResolution() methods to set the horizontal and vertical resolution of the image to 400.
    • Use the getBackgroundColor() method to set the background color of the image to AliceBlue.
  3. 使用 convertEPUB(stream, options, savePath) 方法将 EPUB 文件保存为 PNG 图像。该方法接收 streamoptions、输出文件路径 savePath 并执行转换操作。
 1// Convert EPUB to PNG 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// Initialize ImageSaveOptions
 7ImageSaveOptions options = new ImageSaveOptions();
 8options.setBackgroundColor(Color.getAliceBlue());
 9options.setSmoothingMode(SmoothingMode.HighQuality);
10options.setVerticalResolution(Resolution.to_Resolution(400));
11options.setHorizontalResolution(Resolution.to_Resolution(400));
12
13// Call the convertEPUB() method to convert EPUB to PNG
14Converter.convertEPUB(fileInputStream, options, "input-options.png");

For further information on how to customize the conversion process with ImageSaveOptions you can refer to the Fine-Tuning Converters article.

结论

使用 Aspose.HTML library for Java 将 EPUB 转换为 PNG 既简单又强大。您可以使用 ImageSaveOptions类自定义输出的各个方面,包括分辨率、背景和页面布局。提供的 Java 示例说明了基本和高级转换工作流程,为开发人员提供了有效处理各种情况和要求的工具。

Aspose.HTML提供免费的在线 EPUB到PNG转换器,可将EPUB转换为高质量的PNG,简单快捷。只需上传、转换文件,几秒钟就能得到结果!

文本 “EPUB 到 PNG 转换器”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.