Convert Pdf to Image file using Java

You may also like to read:

Introduction

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.

Prerequisites

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="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.jeejava</groupId>
	<artifactId>pdf-image</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>pdf-image</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>pdfbox</artifactId>
			<version>1.8.3</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

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.io.File;
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()) {
				destinationFile.mkdir();
				System.out.println("Folder Created -> " + destinationFile.getAbsolutePath());
			}
			if (sourceFile.exists()) {
				PDDocument document = PDDocument.load(sourceDir);
				@SuppressWarnings("unchecked")
				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);
					pageNumber++;
				}
				document.close();
				System.out.println("Image saved at -> " + destinationFile.getAbsolutePath());
			} else {
				System.err.println(sourceFile.getName() + " File does not exist");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

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