How to Convert PNG to PDF in Java (Tutorial)

The PDF format is the most important file format when it comes to sending and receiving data online. PDF stands for "Portable Document Format" which allows a user to preserve the page formatting and data in permanent form. It is important to have a PDF viewer and editor to work with PDFs. PDFs are also helpful in sending multiple images at once in a single new file without any evaluation limitations.

Sometimes, there is a need to create or convert different file/image formats such as GIF to new PDF document in our application programs. As Java is the most popular language and widely used by programmers, the programmers require to work with PDF files in their applications. It is useful in generating reports, invoices or bills, convert PNG image to a PDF as per demand. In Java, it can be a difficult task to work with PDF files. In this article, I'm going to use IronPDF for Java library to create PDF documents from image file format programmatically.

IronPDF - A Java Library

Iron Software engineers have now developed IronPDF for Java language which helps Java developers to create new document, edit, write, resize and manipulate PDF documents. IronPDF allows to work with every aspect of PDF file or PDF conversion. It provides developers with a wide range of features for creating and customizing PDF in Java. It also helps controlling the layout and formatting of the PDF.

IronPDF for Java is built on the capabilities of .NET Framework, which makes it a versatile tool for working with PDFs as compared to other open source libraries. In addition to creating and manipulating PDFs, it primarily helps to convert images and HTML files to PDF and from other file formats as well.

Steps to Convert PNG to PDF in Java


To create PNG to PDF conversion application, you will need the following prerequisites downloaded and installed:

  1. Java Development Kit (JDK): The latest version of JDK must be installed on your computer to compile and run the convert PNG to PDF application. The JDK can be downloaded from the Oracle website.
  2. Maven: Maven needs to be installed as it is a build automation tool used primarily for Java projects. The Maven can be downloaded from the Apache Maven website.
  3. IronPDF Java Library: Now you will need the IronPDF's latest version added as dependency to your PNG to PDF conversion project. Add the following IronPDF dependency to your project's pom.xml file:

  4. You will also need to add Slf4j dependency in the pom.xml file.


Once all the prerequisites are downloaded and installed, the project can now be used to convert PNG images to PDF document in Java applications.

Add Imports to Java Main File

First of all, you will need all the following imports for converting PNG images to PDF file using IronPDF in your Java application.

import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

Once the imports are added, you are good to go with the image to PDF conversion.

Convert Single PNG to PDF File

IronPDF helps developers to convert a single PNG or JPG image to PDF file with few simple steps. The Java code snippet is shown in the following example:

// Reference to the image that we want to convert
List<Path> paths = new ArrayList<>();

// Save the image into a new PDF document.

Here, you first need to get the path of the image file which you are trying to convert to PDF. Then, you add that image path into a List and call the fromImage method. Finally, call the saveAs method to save the converted PNG file to the filesystem.

Convert Multiple PNGs into a PDF File

IronPDF also allows developers to combine multiple PNG or JPG images into a single PDF document as shown in the following code example.

// Reference to the directory containing the images that we desire to convert
Path imageDirectory = Paths.get("assets/images");

// Create an empty List to contain Paths to images from the directory.
List<Path> imageFiles = new ArrayList<>();

// Use a DirectoryStream to populate the List with paths for each image in the directory
try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png, jpg, gif}")) {
    for (Path entry : stream) {

    // Render all targeted images as PDF content and save them together in one PDF document.
} catch (IOException exception) {
    throw new RuntimeException(String.format("Error converting images to PDF from directory: %s: %s",

The fromImage method in the above code can also accept a list of PNG image path objects. Each path object reference a valid path to an image stored locally. In the case, the images are from the same directory, so a DirectoryStream found in java.nio.file classes is quickly used to build up a list of all the images contained in the directory.  

Now, the fromImage method will render each PNG or JPG image present in the list on a separate page of the PDF document. Finally, use saveAs method to save the converted images to multiple_images.pdf output.

After running the project with any of the above code example, the output is generated in PDF file format. Here the output shown is from "Convert PNG to PDF" Multiple Images.

How to Convert PNG to PDF Using Java - Figure 1: PNG to PDF Output

PNG to PDF Output

You can check further code examples on image conversion and also download the IronPDF for Java from this browser link.

How to Convert PNG to PDF Using Java - Figure 2: IronPDF for Java

IronPDF for Java

IronPDF has a great quality of rendering all images and text without losing any format. Buttons are also clickable, and text boxes are editable in the converted PDF file.


In this article, we looked at generating PDF using the convert image to PDF feature of IronPDF. In conclusion, IronPDF is a powerful library developed for Java developers to work with PDF file format. It allows developers to easily create PDF document from multiple images from scratch. It also supports XML documents to PDF along with HTML and image files. IronPDF is a great PDF API, which helps Software Engineers to extract and write content from PDF file. It also helps to convert HTML content or other formats like PNG images to PDF.

IronPDF for Java is free to use but for deployment purpose it has a commercial license which starts from only $749. You can also access the 30-day free trial of full version of IronPDF to test its functionality in production mode.