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 in Java.
- Java Image to PDF Converter - Free Download
- Steps to Convert Image to PDF in Java
- Convert an Image to PDF
- Get a Free API License
Java Image to PDF Converter - Free Download
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 images to PDF seamlessly. 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>
Steps to Convert Image to PDF in Java
Aspose.Imaging for Java provides the simplest way of converting images of popular formats to PDF. The following are the steps to convert the image(s) to PDF in Java.
- Create a new PDF and add pages into it.
- Load the images from files one by one.
- Add each image to PDF.
- Save PDF to desired location.
Let’s now transform the above-mentioned steps into code and save an image as PDF using Java.
Save an Image as 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
Java API to Convert Image to PDF - Get a Free 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.