PDF is among the widely opted file formats for printing and sharing digital documents. With a number of useful features, the PDF format allows you to create rich content having text, graphics, annotations, etc. However, you may need to convert the PDF files to image formats, e.g. JPG, in certain cases. To accomplish that programmatically, this article covers how to convert PDF to JPG images in Java.
Java PDF to JPG Converter API – Free Download
Aspose.PDF for Java is a powerful PDF generation and manipulation API. With a wide range of features, you can seamlessly create, process, and convert PDF files of simple and complex layouts. We will utilize this API to convert PDF files to JPG images in this article. You can download the API’s JAR or install it using the following Maven configurations.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.4</version>
</dependency>
PDF to JPG Java Conversion
The following are the steps to convert the pages in a PDF document to JPG images using Aspose.PDF for Java.
- Load the PDF file using Document class.
- Loop through the pages of the PDF using Document.getPages() method.
- In each iteration, create a FileOutputStream object for the output JPG image.
- Create and initialize an object of JpegDevice class.
- Convert page to JPG using JpegDevice.process(Page, FileOutputStream) method.
The following code sample shows how to convert pages in PDF to JPG in Java.
Convert Single Page of PDF to JPG
You can also convert only a single page of PDF to JPG. In that case, you can access the desired page from the collection. The following are the steps to convert only a single page of PDF to JPG.
- Load the PDF file using the Document class.
- Create FileOutputStream for the output JPG image.
- Create and initialize the JpegDevice object.
- Convert page to JPG using JpegDevice.process(Page, FileOutputStream) method.
The following code sample shows how to convert a single page in PDF to JPG.
Conclusion
In this post, you have learned how to convert pages in a PDF file to JPG images using Java. The code samples have shown how to convert all or desired pages of PDF to JPG images. In addition, you can explore more about Java PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.