If you need to generate a PDF from images, you have landed on the right page. Because this article provides an easy yet powerful method of converting images to PDF in Java. Not only this, the provided method takes care of the high-quality image to PDF conversion within no time. So let’s see how to perform image to PDF conversion in Java.
- Java Image to PDF Conversion Library
- Convert an Image to PDF in Java
- Convert Multiple Images to a PDF in Java
Java Library to Convert Image to PDF
To convert image to PDF, we will use Aspose.PDF for Java. It is a PDF manipulation library that you can use to create, modify, and process PDF files. Furthermore, its high-quality converter lets you convert PDF and other documents seamlessly.
You can either download the library or install it using the following Maven configurations.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.12</version>
</dependency>
How to Convert an Image to PDF in Java
In this section, we’ll see how to convert an image to a PDF file in Java. This can be useful when you need to convert each image into a separate PDF file. The following are the steps to perform image to PDF conversion in Java.
- Create an instance of Document class.
- Add a new Page to the PDF document using Document.getPages().add() method and set page’s margins.
- Load the image file into a FileInputStream object.
- Add a new Image to the page using Page.getParagraphs().add(Image) method.
- Set the image’s stream using Image.setImageStream() method.
- Finally, save the PDF document using Document.save(String) method.
The following code sample shows how to convert a PNG image to a PDF in Java.
Convert Multiple Images to a PDF in Java
The following are the steps to convert multiple images to a single PDF file in Java.
- Create an instance of Document class.
- Get the list of the image files from the folder and for each image, do the following:
- Add a new Page to the PDF document using Document.getPages().add() method and set page’s margins.
- Load the image file into a FileInputStream object.
- Add a new Image to the page using Page.getParagraphs().add(Image) method.
- Set the image’s stream using Image.setImageStream() method.
- Finally, save the PDF document using Document.save(String) method.
The following code sample shows the conversion of multiple images to a single PDF in Java.
Free Java Image to PDF Conversion
You can convert images to PDF without evaluation limitations by getting a free temporary license.
Explore Java PDF Library
You can explore more about Aspose.PDF for Java using documentation. In case you would have any queries, feel free to let us know via our forum.
Conclusion
In this article, you have learned how to convert an image to PDF format in Java. Also, you have seen how to convert multiple images to a single PDF programmatically. Thus, you can easily perform any kind of image-to-PDF conversion in your application.