This example shows how to create an image from each page of a PDF. Instead of the ImageIOUtil? class one could also have used Java's built-in JavaDoc:javax.imageio.ImageIO class.


import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

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

public class ConvertToImage {

    public static void main (String[] args) throws Exception {
        PDDocument doc = PDDocument.load(new File(args[0]));
        PDFRenderer pdfRenderer = new PDFRenderer(doc);
        int pageCounter = 0;
        for (PDPage page : doc.getPages())
        { 
            BufferedImage bi = pdfRenderer.renderImageWithDPI(pageCounter, 300ImageType.RGB);
            ImageIOUtil.writeImage(bi, args[0]+"-"+(pageCounter++)+".png"300);
        }
        doc.close();
    }
}


CodeSnippets