Convert PNG to PDF using Java

Convert PNG to PDF using Java

PNG and PDF are popular and the most widely used file formats at the current point in time. PNG is an image file format whereas PDF(Portable Document Format) offers reliable and efficient data representation. You need to convert the image file format to PDF in some scenarios. Therefore, in this blog post, we will learn how to convert PNG to PDF using Java PDF API. We will write the code snippet and the steps to perform this conversion in a Java application.

We will cover the following topics in this article:

Java PDF library

This Java PDF library is easy to install and offers documentation regarding installation. It is an enterprise-level API that offers robust conversion and manipulation features. However, you can download the JAR or install it using the following Maven configurations:

    <name>Aspose Java API</name>

Convert PNG to PDF using Java

We are going to use the classes and methods exposed by this Java PDF library. It lets you perform PNG to PDF conversion by writing a few lines of source code in Java.

You may follow the steps and the code snippet mentioned below:

  1. Instantiate an instance of the Document class.
  2. Create an object of DocumentBuilder class to make it simple to add content to the document.
  3. Load the input image file by calling the createImageInputStream method and assign it to the object of ImageInputStream.
  4. Invoke the getImageReaders method that returns an Iterator containing all currently registered ImageReaders and assigns it to the object of ImageReader class.
  5. Call the setInput method that sets the input source to use to the given ImageInputStream.
  6. Get the number of frames in the image by calling the getNumImages method.
  7. Loop through all frames.
  8. Select an active frame and assign it to the object of BufferedImage class.
  9. Invoke the getPageSetup method to access the current page setup and assign it to the object of the PageSetup class.
  10. Set the page height by calling the setPageWidth method.
  11. Invoke the setPageHeight method to set the width of the page.
  12. Insert the image into the document and position it at the top left corner of the page by calling the insertImage method.
  13. Save the file as a PDF file format by calling the save method.

You can see the output in the image below:

PNG to PDF conversion

PNG to PDF conversion

Get a Free License

You can get a free temporary license to try the API beyond evaluation limitations.

Summing up

We can end this blog post here. We have demonstrated the implementation of the Java PDF library to convert PNG to PDF using Java programmatically. This library lets you build a PNG to PDF converter using a few lines of code. In addition, you may visit the documentation of this PNG to PDF conversion API. is continuously writing on new interesting topics. Therefore, please stay connected for the latest updates.

Ask a question

You can let us know about your questions or queries on our forum.


How do you convert a PNG to a PDF?

You can use this library to convert PNG to PDF in Java programmatically. It offers comprehensive documentation regarding its usage.

How do I save a PNG as a PDF without losing quality?

Go through this section to learn how to save a PNG as a PDF file using a Java library. You can invoke the save method to save a PNG file as a PDF file.

See Also