PDF to Image Conversion in Java

PDF to Image Conversion in Java

PDF, PNG, and JPEG are widely used and renowned file formats for data storage and representation. All three have their strengths and weaknesses. PDF is based on vector files whereas, PNG and JPEG are raster image files. In this blog post, we will dive and learn how to achieve PDF to image conversion in Java programmatically. To accomplish this, we will convert PDF to PNG and PDF to JPEG by installing a simple Java library in a Java application.

The following points will be covered in this blog post:

PDF to Image Conversion in Java - API installation

We will use an enterprise-level Java library to convert PDF files to images. This library offers a wide range of file format conversion and file manipulation features. So, to enable this library, you can either download the jar files or follow 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>20.12</version>
    <classifier>jdk17</classifier>
</dependency>

Convert PDF to PNG using the Java library

In this section, we will convert PDF file to PNG image format using a few lines of source code in the Java application. In addition, we can configure the API calls as per requirements.

We will perform the following steps to convert PDF to PNG programmatically:

  1. Initialize an object of the Document class and load the PDF file.
  2. Loop through all the pages of PDF pages using getPages() and create a stream object to save the output image.
  3. Create a Resolution object and set a value.
  4. Create a PngDevice object with the defined resolution.
  5. Invoke the process method to convert PDF to PNG and save the image to stream.
  6. Call the close() method to close the stream.

Now, copy and paste the following code snippet in your Java file:

How to convert PDF to JPEG programmatically

Convert PDF file to another famous image format JPEG by following the steps mentioned below:

  1. Create an object of the Document class and load the PDF file.
  2. Loop through all the pages of PDF pages using getPages() and create a stream object to save the output image.
  3. Create an object of the Resolution class and set a value.
  4. Create a JpegDevice object with the defined resolution.
  5. Call the process method to convert PDF to JPEG and save the image to stream.
  6. Invoke the close() method to close the stream

Copy and paste the following code snippet to convert PDF to JPEG in Java Programmatically:

Get a Free License

Please avail a free temporary license to try the API without evaluation limitations.

Summing up

This blog post ends here. We have learned how to achieve PDF to Image conversion in Java. Moreover, we have demonstrated how to convert PDF to PNG and PDF to JPEG programmatically in a Java application. In addition, you may explore the other features yourself by diving into the documentation. Finally, conholdate.com is in a consistent process of writing new blog posts. Therefore, please stay in touch for the latest updates.

Ask a question

You can share your questions or queries on our forum.

See Also