Convert PDF pages to Images - Apache PDFBox example

Convert PDF pages to Images - Apache PDFBox example

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

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 PDFToImage {

 public PDFToImage() {

 }

 public static void main(String[] args) {

  try {

   PDDocument pdDocument = new PDDocument();
   PDDocument oDocument = PDDocument.load(new File("docs/my_form.pdf"));
   PDFRenderer pdfRenderer = new PDFRenderer(oDocument);
   int numberOfPages = oDocument.getNumberOfPages();

   for (int i = 0; i < numberOfPages; i++) {
    BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 300, ImageType.RGB);
    String filename = "docs/my_form_image" + "_" + i + ".png";
    ImageIOUtil.writeImage(bim, filename, 300);

   }

   pdDocument.close();

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


 }

}

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.