Convert EPUB to JPG in Java

EPUB is the most widely supported e-book format. An EPUB file is a ZIP archive that actually contains a website, including HTML files, images, CSS style sheets, and other assets. Converting EPUB to JPG 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 image formats, 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 image format specifying. You can set the 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 JPG using Aspose.HTML for Java and how to apply ImageSaveOptions and MemoryStreamProvider. You can easily use Java examples to convert EPUB to JPG, detailed here for converting EPUB to PNG, EPUB to BMP, EPUB to GIF, and EPUB to TIFF. Just set the ImageFormat to ImageSaveOptions!

EPUB to JPG 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 JPG in your Java application literally with a few lines of code!

1    // Open an existing EPUB file for reading
2    final  com.aspose.html.internal.ms.System.IO.FileStream stream = com.aspose.html.internal.ms.System.IO.File.openRead(StringExtensions.concat(getDataDir(),  "input.epub"));
3    try
4    {;
5    }
6    finally { if (stream != null) stream.dispose(); }
7
8    // Invoke the convertEPUB() method
9    com.aspose.html.converters.Converter.convertEPUB(stream, new ImageSaveOptions(ImageFormat.Jpeg), Path.combine(getOutputDir(), "output-image.jpg"));   

Convert EPUB to JPG

Let’s walk through the step-by-step instructions for a simple EPUB to JPG conversion scenario:

  1. 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.
  2. Create an instance of ImageSaveOptions with JPG ImageFormat. ImageFormat.Png will be used as default image format.
  3. Use the convertEPUB(stream, options, savePath) method of the Converter class to save EPUB as an JPG image. The method takes as parameters stream, options, and savePath and performs the conversion.

Using convertEPUB() methods is the most common way to convert EPUB files into various formats. The following Java code snippet shows how to convert EPUB to JPG using Aspose.HTML:

 1    // Open an existing EPUB file for reading
 2    final  com.aspose.html.internal.ms.System.IO.FileStream stream = com.aspose.html.internal.ms.System.IO.File.openRead(StringExtensions.concat(getDataDir(),  "input.epub"));
 3    try
 4    {;
 5    }
 6    finally { if (stream != null) stream.dispose(); }
 7
 8    // Prepare a path for converted file saving 
 9    String savePath = Path.combine(getOutputDir(), "output.jpg");            
10
11    // Initialize ImageSaveOptions 
12    ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
13
14    // Call the convertEPUB method() to convert EPUB to JPG
15    com.aspose.html.converters.Converter.convertEPUB(stream, options, savePath);

You can download the complete examples and data files from GitHub.

Save Options

Aspose.HTML for Java provides the ability to programmatically convert EPUB to JPG with full control over various conversion options using ImageSaveOptions. This feature allows users to customize the rendering process by setting the image format, page size, margins, compression level, CSS media-type, and other parameters.

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.

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

Convert EPUB to JPG using ImageSaveOptions

Aspose.HTML for Java allows converting EPUB to JPG using default or custom save options. Setting different options of ImageSaveOptions class gives you control over the page size, margins, background color, and resolution settings for the output image. These are the steps to convert an EPUB file to JPG format with ImageSaveOptions specifying:

  1. 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.
  2. Create a new ImageSaveOptions object with JPG ImageFormat and specify the required save options:
    • 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 Alice Blue.
    • Use the setAnyPage() method to set the size of the image and margins.
  3. Use the convertEPUB(stream, options, savePath) method to save EPUB file as a JPG image. The method takes the stream, options, output file path savePath and performs the conversion operation.

The following Java example shows how to use ImageSaveOptions and create a JPG file with custom page-size, margins, resolution, rendering quality, and background color:

 1    // Open an existing EPUB file for reading
 2    final  com.aspose.html.internal.ms.System.IO.FileStream stream = com.aspose.html.internal.ms.System.IO.File.openRead(StringExtensions.concat(getDataDir(),  "input.epub"));
 3    try
 4    {;
 5    }
 6    finally { if (stream != null) stream.dispose(); }
 7    
 8    // Prepare a path for converted file saving 
 9    String savePath = Path.combine(getOutputDir(), "input-options.jpg");
10                    
11    // Initialize an instance of the ImageSaveOptions class
12    ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
13    options.setSmoothingMode(SmoothingMode.HighQuality);
14    options.setHorizontalResolution(new Resolution(400, UnitType.AUTO));
15    options.setVerticalResolution(new Resolution(400, UnitType.AUTO));
16    com.aspose.html.drawing.Color.getAliceBlue().CloneTo(options.getBackgroundColor());
17    options.getPageSetup().setAnyPage(new Page(new com.aspose.html.drawing.Size(800, 500), new Margin(30, 20, 10, 10)));
18
19    // Convert EPUB to JPG
20    com.aspose.html.converters.Converter.convertEPUB(stream, options, savePath);    

Aspose.HTML offers a free online EPUB to JPG Converter that converts EPUB to JPG with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!

Text “Banner EPUB to JPG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.