Saturday 12 March 2022

PdfBox: Convert pdf to png files

In this post, I am going to explain how to convert a pdf to png files.

 

Step 1: Load the pdf document.

PDDocument pdfDocument = PDDocument.load(inputPdfFile);

 Step 2: Traverse the pdf document page by page and convert each page to a png file.

 

PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument);

for (int page = 0; page < pdfDocument.getNumberOfPages(); page++) {
	BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
	String filename = String.format("%s%04d.png", imagesPrefix, page + 1);
	ImageIOUtil.writeImage(bufferedImage, new File(imageDir, filename).getAbsolutePath(), 300);
}




Find the below working application.

 

ConvertPdfToPng.java

package com.sample.app;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Comparator;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;

public class ConvertPdfToPng {

	public static File[] convertPdf2Png(File inputPdfFile, File folderToWrite, String imagesPrefix) throws IOException {

		// Create parent directories
		if (folderToWrite.getParentFile() != null) {
			folderToWrite.mkdirs();
		}

		Path destinationFolderPath = folderToWrite.toPath();
		File imageDir = destinationFolderPath.toFile();

		try (PDDocument pdfDocument = PDDocument.load(inputPdfFile);) {

			PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument);

			for (int page = 0; page < pdfDocument.getNumberOfPages(); page++) {
				BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
				String filename = String.format("%s%04d.png", imagesPrefix, page + 1);
				ImageIOUtil.writeImage(bufferedImage, new File(imageDir, filename).getAbsolutePath(), 300);
			}

		}

		// find the image files
		File[] imageFiles = imageDir.listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				return name.toLowerCase().startsWith(imagesPrefix.toLowerCase()) && name.toLowerCase().endsWith("png");
			}
		});

		if (imageFiles == null) {
			throw new IOException("Error occurred while converting pdf file to images");
		}

		Arrays.sort(imageFiles, new Comparator<File>() {
			@Override
			public int compare(File file1, File file2) {
				return file1.getName().compareTo(file2.getName());
			}
		});

		return imageFiles;
	}

	public static void main(String[] args) throws IOException {

		convertPdf2Png(new File("/Users/Shared/personal/test.pdf"), new File("/Users/Shared/personal/demo/images"),
				"img-");
	}

}

 

When you ran above application, images like below are created in the destination folder.

img-0001.png
img-0002.png
img-0003.png
……..
……..

 

 

 

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment