PDF format is ruling the world of digital documents because of its cross-platform support. However, in certain cases, you have to convert PDF files to other file formats. For such cases, this article covers how to convert PDF files to popular image formats. Particularly, you will learn how to convert PDF to PNG, JPEG, BMP, and TIFF images using Java.
- Java PDF to Image Converter API
- Convert PDF File to PNG using Java
- Convert PDF File to JPEG using Java
- PDF to BMP Conversion using Java
- PDF to TIFF Conversion using Java
- Get a Free API License
Java PDF to Image Converter API
In this article, we’ll use Aspose.PDF for Java which is designed to create new as well as manipulate existing PDF files. The API provides high fidelity conversion of PDF files into various document and image formats. You can either download API’s JAR or install it using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>20.12</version>
<classifier>jdk17</classifier>
</dependency>
Convert PDF File to PNG Image using Java
In PDF to PNG conversion, each page of the PDF file is converted into a separate PNG image. Thus, you can convert a single page, all pages, or specific pages of the PDF as per your scenario. The following are the steps to convert a PDF file to PNG using Java.
- Load the PDF file using Document class.
- Loop through the pages in PDF file using Document.getPages() method.
- Create OutputStream object for each PNG image.
- Instantiate the Resolution class to set the resolution of rendered images.
- Create an object of PngDevice class and initialize it with the Resolution object.
- Use PngDevice.process(Document.getPages().get_Item(Index), OutputStream) method to convert PDF page to PNG image.
- Close the file stream.
The following code sample shows how to convert PDF to PNG image using Java.
Convert PDF File to JPEG Image using Java
The following are the steps to convert pages in a PDF file to JPEG images using Java.
- Load the PDF file using Document class.
- Loop through the pages in PDF using Document.getPages() method.
- Create OutputStream object for each JPEG image.
- Instantiate the Resolution class to set the resolution of rendered images.
- Create an object of JpegDevice class and initialize it with the Resolution object.
- Use JpegDevice.process(Document.getPages().get_Item(Index), OutputStream) method to convert PDF page to JPEG image.
- Close the file stream.
The following code sample shows how to convert PDF pages to JPEG images using Java.
PDF to BMP Image Conversion using Java
The following are the steps to perform PDF to BMP conversion in Java.
- Use the Document class to load the PDF file.
- Iterate through the PDF pages using Document.getPages() method.
- Create OutputStream object for each BMP image.
- Instantiate the Resolution class to set the resolution of rendered images.
- Create an object of BmpDevice class and initialize it with the Resolution object.
- Use BmpDevice.process(Document.getPages().get_Item(Index), OutputStream) method to convert PDF page to BMP image.
- Close the file stream.
The following code sample shows how to perform PDF to BMP image conversion in Java.
PDF to TIFF Conversion using Java
In contrast to the above-mentioned raster image formats, TIFF is a multipage image format. Therefore, you can convert a PDF file to TIFF at once without looping through each page. On the other hand, you can also specify a range of pages in PDF to be converted to TIFF. The following are the steps to convert a PDF file to TIFF using Java.
- Use the Document class to load the PDF file.
- Create OutputStream object for TIFF image.
- Instantiate the Resolution class to set the resolution of rendered images.
- Use TiffSettings class to set additional options such as compression type, color depth, etc.
- Create an object of TiffDevice class and initialize it with the Resolution object.
- Use TiffDevice.process(Document, OutputStream) method (or other overload methods to specify a range of pages) to convert PDF to TIFF.
- Close the file stream.
The following code sample shows how to convert a PDF file to TIFF image using Java.
Get a Free API License
You can try the API without evaluation limitations for free. Get a free temporary license now.
Info: Using Aspose JPG to PPT or PNG to PPT converter, you can generate PowerPoint presentations from simple images.
Conclusion
In this article, you have learned how to convert PDF files to image formats using Java. The steps and code samples have shown how to perform PDF to PNG, PDF to JPEG, PDF to BMP, and PDF to TIFF conversion. You can explore more about the Java PDF API using documentation.