Convert Pdf to Image file using Java

You may also like to read:


This tutorial will show you how to convert pdf to image file using Java. For this I am using here pdfbox API. Java pdf to image example will show you step by step conversion procedure.

You may also like to read Convert Image to PDF using Java.


Knowledge of Java, JDK 1.8

Have JDK installed and configured

Have maven installed and configured

Example with Source Code

Here we will create maven based standalone project in Eclipse.

Creating Project

Create maven based standalone project in Eclipse with below artifact and group id.

artifact id: pdf-image, group id: com.jeejava

Updating pom.xml

We need only one dependency called pdfbox for converting pdf to image file.

<project xmlns=""






Java Class

The below Java class converts PDF file into Image file. The output image file will be PNG type.

package com.jeejava.image.pdf;

import java.awt.image.BufferedImage;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public class ConvertPdfToImage {

	public static void main(String[] args) {
		try {
			String sourceDir = "C:/Desktop/sample.pdf";
			String destinationDir = "C:/Desktop/";
			File sourceFile = new File(sourceDir);
			File destinationFile = new File(destinationDir);
			if (!destinationFile.exists()) {
				System.out.println("Folder Created -> " + destinationFile.getAbsolutePath());
			if (sourceFile.exists()) {
				PDDocument document = PDDocument.load(sourceDir);
				List<PDPage> list = document.getDocumentCatalog().getAllPages();

				String fileName = sourceFile.getName().replace(".pdf", "");
				int pageNumber = 1;
				for (PDPage page : list) {
					BufferedImage image = page.convertToImage();
					File outputfile = new File(destinationDir + fileName + "_" + pageNumber + ".png");
					ImageIO.write(image, "png", outputfile);
				System.out.println("Image saved at -> " + destinationFile.getAbsolutePath());
			} else {
				System.err.println(sourceFile.getName() + " File does not exist");

		} catch (Exception e) {


First we get the source from where we want to read the pdf file and destination directory where we want to write the converted image file.

Next we create the required destination directories if they do not exist.

Then we read the pdf file and retrieve all pages and for each page we generate the image file in the destination directory.

Testing the Application

Input pdf file

Output Image file

Source Code

You can download source code.

You may also like to read Image to pdf using Java

Thanks for reading.

Convert Pdf to Image file using Java

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top