PDF has become a universal format to share documents over the internet. Therefore, various other file formats are converted to PDF before they are exchanged. One of the popular conversions is image to PDF that may include PNG to PDF, JPEG to PDF, BMP to PDF, etc. Therefore, this article covers how to convert images to PDF using Java.
Java Image to PDF Converter API
Aspose.PDF for Java is a popular PDF manipulation API that lets you create and process PDF files. Furthermore, it allows you to convert PNG, JPEG, BMP, and other image files to PDF using Java. You can either download the API or install it using the following Maven configuration.
<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.3</version>
</dependency>
Convert an Image to PDF in Java
The following are the steps to convert a PNG, JPEG, BMP, or TIFF image to PDF.
- Create an object of Document class.
- Add a new page to document using Document.getPages().add() method and get its reference in a Page object.
- Create an object of the Image class.
- Set image using Image.setFile(String) method by providing the image file’s path.
- Add image to the PDF’s paragraph using Page.getParagraphs().add(Image) method.
- Save the PDF file using Document.save(String) method.
The following code sample shows how to convert a PNG, JPEG, BMP, or TIFF image to PDF in Java.
Image to PDF
Get a Free API License
You can get a free temporary license to use the API without evaluation limitations.
Conclusion
In this article, you have learned how to convert images to PDF using Java. You can use the provided code sample to convert PNG, BMP, JPEG, and other popular images to PDF. In case you want to explore Aspose.PDF for Java, you can consult the documentation. Furthermore, you can post your queries to our forum.