
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
- Convert PDF to PNG using the Java library
- How to convert PDF to JPEG programmatically
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:
- Initialize an object of the Document class and load the PDF file.
- Loop through all the pages of PDF pages using getPages() and create a stream object to save the output image.
- Create a Resolution object and set a value.
- Create a PngDevice object with the defined resolution.
- Invoke the process method to convert PDF to PNG and save the image to stream.
- 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:
- Create an object of the Document class and load the PDF file.
- Loop through all the pages of PDF pages using getPages() and create a stream object to save the output image.
- Create an object of the Resolution class and set a value.
- Create a JpegDevice object with the defined resolution.
- Call the process method to convert PDF to JPEG and save the image to stream.
- 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.