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:
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>21.11</version> <type>pom</type> </dependency>
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:
- Instantiate an instance of the Document class.
- Create an object of DocumentBuilder class to make it simple to add content to the document.
- Load the input image file by calling the createImageInputStream method and assign it to the object of ImageInputStream.
- Invoke the getImageReaders method that returns an Iterator containing all currently registered ImageReaders and assigns it to the object of ImageReader class.
- Call the setInput method that sets the input source to use to the given ImageInputStream.
- Get the number of frames in the image by calling the getNumImages method.
- Loop through all frames.
- Select an active frame and assign it to the object of BufferedImage class.
- Invoke the getPageSetup method to access the current page setup and assign it to the object of the PageSetup class.
- Set the page height by calling the setPageWidth method.
- Invoke the setPageHeight method to set the width of the page.
- Insert the image into the document and position it at the top left corner of the page by calling the insertImage method.
- Save the file as a PDF file format by calling the save method.
You can see the output in the image below:
Get a Free License
You can get a free temporary license to try the API beyond evaluation limitations.
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. conholdate.com 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?