Generate PDF File from Images using Java

In various cases, you may need to convert a collection of images to a PDF file. For example, you may want to insert all images from an album into a single PDF file. On the other hand, you may need to generate PDF from the images of a scanned document. In order to perform image to PDF conversion, this article covers how to generate a PDF from multiple images using Java.

Java API for Image to PDF Conversion

Aspose.PDF for Java is a powerful API that provides basic as well as advanced PDF manipulation features. It lets you generate new PDF files and modify the existing ones quite easily. You can download the API’s JAR or install it using the following Maven configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>21.2</version>
</dependency>

Steps to Generate PDF from Images

The following are the steps that we will follow to generate a PDF file from images in this article.

  • First, create a new PDF document.
  • Get the list of the images’ file names in an array.
  • For each image file in the list, do the following:
    • Add a new page to the PDF document and set its properties.
    • Load each image file in a file stream from the list.
    • Add image to the paragraph collection of the page.
  • Finally, save the PDF document.

Generate PDF from Images using Java

Let’s now check out how to implement each of the above-mentioned steps using Aspose.PDF for Java.

The following code sample shows how to generate PDF file from images in Java.

Get a Free API License

In case you want to try the API without evaluation limitations, you can get a free temporary license.

Conclusion

In this article, you have learned how to generate a PDF file based on multiple images using Java. The step-by-step guide and code sample demonstrated how to fetch images from a folder and insert them into the PDF. You can explore more about the Java PDF API using documentation.

See Also