
If you need to generate a PDF from one or more images, you’re in the right place. This article presents a straightforward yet powerful approach for converting images to PDF in Java using Aspose.PDF. The solution delivers high‑quality image‑to‑PDF conversion quickly and efficiently. Let’s explore 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 images to PDF in Java, we use Aspose.PDF for Java, a comprehensive PDF manipulation library. Aspose.PDF enables you to create, edit, and process PDF documents programmatically. Its high‑quality converter ensures that image‑to‑PDF transformations retain original resolution and color fidelity.
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 demonstrate how to convert a single image file into an individual PDF document. This is useful when each image must be packaged separately. Follow these steps for reliable 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
To combine several images into one PDF document, follow these steps. This approach creates a multi‑page PDF where each image occupies its own page.
- 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 obtain a free temporary license to remove evaluation limits and enable unrestricted image‑to‑PDF conversion with Aspose.PDF for Java by getting a free temporary license.
Explore Java PDF Library
Explore the full capabilities of Aspose.PDF for Java through the official documentation. If you have questions, post them on our forum for assistance.
Conclusion
In this article you learned how to convert a single image and how to merge multiple images into a PDF using Java. With Aspose.PDF you can implement fast, high‑quality image‑to‑PDF conversion in any Java application.