This simple tutorial discusses the details on how to convert image to PDF in Java. By using Java PNG to PDF conversion features can easily be incorporated into an application by following the given steps and code snippet. Moreover, embedding this feature into your applications does not require any additional image processing software.
Steps to Convert Image to PDF in Java
- Add the reference to Aspose.PDF for Java library in the project for converting PNG to PDF
- Create an instance of the Document class to create an empty PDF document
- Create an object of the Image class
- Load the source image in JPG or PNG format for rendering it to PDF format
- Write the converted image as a PDF document
To convert PNG to PDF Java environment based configuration and coding steps are described here. All necessary classes are introduced that are required to perform this conversion like Document and Image classes. These classes let you convert different kinds of images including JPG, BMP, PNG, etc. to a PDF document.
Code to Convert Image to PDF using Java
These lines of code in Java convert PNG to PDF by loading the image from the disk and then getting the reference to the first page in the newly created PDF file. Each page contains a collection of paragraphs where you can add content including the images. Once the loaded PNG image is added to this collection, you can save the resultant document as a PDF on the disk.
In this article, we have explored the PNG to the PDF conversion process. However, if you are interested to learn the reverse process to convert a PDF to an image, please refer to the article on how to convert PDF to Image in Java.