This short tutorial provides guidance on how to convert PDF to image in Java. While writing this PDF to image converter Java code is used containing a few lines of code for loading the source PDF and then configuring the output image properties. In the final step, each page is saved as a JPG file.
Steps for PDF to Image Conversion in Java
- Add a reference to Aspose.PDF from the Maven repository for PDF to image conversion
- Load the source PDF file into the Document class object for converting to JPG
- Set the desired resolution of the output image using the Resolution class object
- Initialize the respective image device based on the required output image type and set resolution
- Iterate through all the pages in the source PDF file
- Save each page as a separate image file using the Process function in the JpegDevice class
During the process of PDF to image conversion in Java first required library references are added. Then the source PDF file is loaded and the resolution of the output image is set in the respective ImageDevice class like JpegDevice used in this tutorial. You can render the selected page(s) to the image files as per the requirement.
Code to Convert PDF to Image in Java
In this tutorial to convert PDF to JPG Java code is used that loads the source PDF file from disk to Document class object however you can load PDF files from a byte array and input stream also. JpegDevice class is used to set the resolution and then create images for each page using the defined resolution. If you want to create other types of images, use respective device classes like BmpDevice, EmfDevice, GifDevice, PngDevice, and many others.
This tutorial has guided us on how to convert PDF to Image in Java. If you are interested in converting PDF to HTML, refer to the article on how to convert PDF to HTML in Java.