Extract Image From PDPage – PDFBox

In this tutorial we learn extract image from PDPage – PDFBox, the output image generated from this process represent a screenshot of PDPage.
I have build a wrapper which contain BufferedImage and PDPage to reference of this image. The method extractorScreenshots need List of PDPage and the entire PDDocument for build PDFRenderer.

Orientamento PDPage in PDFBox 2

PDFBox 2 Change page orientation


import java.awt.image.BufferedImage;
import org.apache.pdfbox2.pdmodel.PDPage;
 
// Wrapper class with BufferedImage extracted.
public class PDFBleedImageWrapper {
 
	private BufferedImage screenImage;
	private PDPage pdPage;
 
	public PDFBleedImageWrapper(BufferedImage screenImage, PDPage pdPage) {
		this.screenImage = screenImage;
		this.pdPage = pdPage;
	}
 
	public BufferedImage getScreenImage() {
		return screenImage;
	}
 
	public void setScreenImage(BufferedImage screenImage) {
		this.screenImage = screenImage;
	}
 
	public PDPage getPdPage() {
		return pdPage;
	}
 
	public void setPdPage(PDPage pdPage) {
		this.pdPage = pdPage;
	}
 
}
 
private List<PDFBleedImageWrapper> extractorScreenshots(List<PDPage> renderPages, PDDocument document){
	PDFRenderer pdfRenderer = new PDFRenderer(document);
	List<PDFBleedImageWrapper> bleedImages = new ArrayList<PDFBleedImageWrapper>();
	for (int numPage = 0; numPage < renderPages.size(); ++numPage){ 
		PDPage pageProcessing = renderPages.get(numPage);
		BufferedImage extractedScreenshot = null;
		try{
			extractedScreenshot = pdfRenderer.renderImage(numPage, 1, ImageType.RGB);
		}catch(IOException e){
			logger.error("Error during extract screenshot", e);
		}
 
		if(extractedScreenshot!=null){
			bleedImages.add(new PDFBleedImageWrapper(extractedScreenshot, pageProcessing));
		}
	}
 
	return bleedImages;
}
Extract Image From PDPage – PDFBox ultima modidfica: 2018-02-16T11:41:31+01:00 da Gianluca Di Vincenzo
Posted in: Java

By on 16 Febbraio 2018

Tagged: , , , , ,