Convert EPUB to PNG in Java
EPUB is the most widely supported e-book format. Converting EPUB to PNG can be helpful when you want to include an EPUB file in a PowerPoint presentation or send it by email. Or, for example, you want to share an EPUB file with someone who doesn’t have an EPUB reader installed.
Aspose.HTML for Java library provides a wide range of EPUB conversions to images, such as JPG, PNG, BMP, TIFF, and GIF. Converting EPUB to any supported image format follows the same mandatory steps:
- Opening an EPUB file.
- Creating a save options object.
- Converting EPUB to chosen image format.
The only difference is in specifying the output image format using the
ImageSaveOptions class. The ImageSaveOptions(format)
constructor initializes the options object with the format specifying. You can set the image format to JPG, PNG, BMP, GIF, or TIFF. The default ImageFormat
is PNG.
In this article, you will find information on how to convert an EPUB to PNG using Aspose.HTML for Java and how to apply
ImageSaveOptions and
MemoryStreamProvider. You can easily use Java examples to convert EPUB to PNG, detailed here for converting
EPUB to JPG, EPUB to BMP, EPUB to GIF, and EPUB to TIFF. Just set the ImageFormat
to ImageSaveOptions!
EPUB to PNG by a few lines of Java code
The static methods of the Converter class are primarily used as the easiest way to convert an EPUB file into various formats. You can convert EPUB to PNG in your Java application literally with a single line of code!
1// @md products/html/en/java/converting-between-formats/epub-converter/convert-epub-to-png/_index.md
2// Open an existing EPUB file for reading.
3Converter.convertEPUB(new java.io.FileInputStream($i("input.epub")), new ImageSaveOptions(), $o("convert-with-single-line.png"));
1 // @java java/com/aspose/html/examples/Examples_Java_ConvertingBetweenFormats_ConvertEPUBToPNG_WithASingleLine.java
2 // Open an existing EPUB file for reading
3 final FileStream stream = File.openRead(StringExtensions.concat(getDataDir(), "input.epub"));
4
5 // Convert EPUB to PNG
6 Converter.convertEPUB(stream, new ImageSaveOptions(), "convert-by-few-lines.png"));
Convert EPUB to PNG
Let’s walk through the step-by-step instructions for a simple EPUB to PNG conversion scenario:
- Open an existing EPUB file. In the example, we use the
openRead()
method to open and read an EPUB file from the file system at the specified path. - Create an instance of
ImageSaveOptions.
ImageFormat.Png
will be used as default image format. - Use the
convertEPUB(stream, options, savePath)
method of the Converter class to save EPUB as an PNG image. The method takes as parametersstream
,options
, andsavePath
and performs the conversion.
The following Java code snippet shows how to convert EPUB to PNG using Aspose.HTML:
1// @md products/html/en/java/converting-between-formats/epub-converter/convert-epub-to-png/_index.md
2// Opens an existing EPUB file for reading.
3java.io.FileInputStream fileInputStream = new java.io.FileInputStream($i("input.epub"));
4// Create an instance of the ImageSaveOptions class
5ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
6// Call the ConvertEPUB() method to convert EPUB to PNG
7Converter.convertEPUB(fileInputStream, options, $o("input-output.png"));
1 // @java java/com/aspose/html/examples/Examples_Java_ConvertingBetweenFormats_ConvertEPUBToImage_ConvertEPUBToPNG.java
2 // Open an existing EPUB file for reading
3 FileStream stream = File.openRead("input.epub");
4
5 // Prepare a path for converted file saving
6 String savePath = "output.png");
7
8 // Initialize ImageSaveOptions
9 ImageSaveOptions options = new ImageSaveOptions();
10
11 // Call the convertEPUB() method to convert EPUB to PNG
12 Converter.convertEPUB(stream, options, savePath);
You can download the complete examples and data files from GitHub.
Save Options
ImageSaveOptions allows users to customize the rendering process by setting the image format, page size, margins, compression level, CSS media-type, and other parameters.
Method | Description |
---|---|
setCompression(value) | Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW . |
getCss | Gets 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. |
Convert EPUB to PNG using ImageSaveOptions
Aspose.HTML for Java allows converting EPUB to PNG using default or custom save options. These are the steps to convert an EPUB file to PNG format with ImageSaveOptions specifying:
- Open an existing EPUB file. Use the
openRead()
method of System.IO.FileStream class to open and read source files from the file system at the specified path. - Create a new
ImageSaveOptions object with PNG ImageFormat and specify the required save options:
- Use the
setSmoothingMode()
method to set the quality of the image smoothing to high. - Use the
setHorizontalResolution()
andsetVerticalResolution()
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 Alice Blue.
- Use the
- Use the
convertEPUB(stream, options, savePath)
method to save EPUB file as a PNG image. The method takes thestream
,options
, output file pathsavePath
and performs the conversion operation.
The following Java example shows how to use ImageSaveOptions
and create a PNG file with custom rendering quality, resolution, and background color:
1// @md products/html/en/java/converting-between-formats/epub-converter/convert-epub-to-png/_index.md
2// Open an existing EPUB file for reading.
3java.io.FileInputStream fileInputStream = new java.io.FileInputStream($i("input.epub"));
4
5// Initialize ImageSaveOptions
6ImageSaveOptions options = new ImageSaveOptions();
7options.setSmoothingMode(SmoothingMode.HighQuality);
8options.setHorizontalResolution(Resolution.to_Resolution(400));
9options.setBackgroundColor(Color.getMistyRose());
10options.setVerticalResolution(Resolution.to_Resolution(400));
11options.setBackgroundColor(Color.getAliceBlue());
12
13// Call the ConvertEPUB() method to convert EPUB to PNG
14Converter.convertEPUB(fileInputStream, options, $o("input-options.png"));
1 // @java java/com/aspose/html/examples/Examples_Java_ConvertingBetweenFormats_ConvertEPUBToPNG_WithImageSaveOptions.java
2 // Open an existing EPUB file for reading
3 FileStream stream = File.openRead("input.epub");
4
5 // Prepare a path for converted file saving
6 String savePath = "input-options.png");
7
8 // Initialize ImageSaveOptions
9 ImageSaveOptions options = new ImageSaveOptions();
10 options.setSmoothingMode(SmoothingMode.HighQuality);
11 options.setHorizontalResolution(new Resolution(400, UnitType.AUTO));
12 options.setVerticalResolution(new Resolution(400, UnitType.AUTO));
13 options.setBackgroundColor(Color.getAliceBlue())
14
15 // Convert EPUB to PNG
16 Converter.convertEPUB(stream, options, savePath);
For further information on how to customize the conversion process with ImageSaveOptions you can refer to the Fine-Tuning Converters article.
Aspose.HTML offers a free online EPUB to PNG Converter that converts EPUB to PNG with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!