Showing posts with label Apache. Show all posts
Showing posts with label Apache. Show all posts

Wednesday, June 27, 2012

Useful API converting PDF to image

1. Download the tool PDFBox (e.g. pdfbox-app-1.7.0.jar) via the following URL:


2. Include the library in your java program. The following source code is attached for your reference.



package com.test;

import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFImageWriter;

public class PDFBoxPreviewTest {

       /**
        * @param args
        */
       public static void main(String[] args) {
              try {
                     String path = "D:\\temp\\a.pdf";
                     PDFBoxPreviewTest t = new PDFBoxPreviewTest();
                     t.getDocument(path);
              } catch (Exception e) {

                     System.out.println(e.getMessage());
              }
       }

       private PDDocument getDocument(String path) throws Exception {
              PDDocument document = null;

              document = PDDocument.load(path);

              PDFImageWriter writer = new PDFImageWriter();
              boolean success = writer.writeImage(document, "jpg", "", 1, 1,
                           "myFileName", BufferedImage.TYPE_INT_RGB, Toolkit
                                         .getDefaultToolkit().getScreenResolution());
              System.out.println(success);

              return document;
       }
}

3. myFileName.jpg will be generated!


Source Code can be found here:
http://test-pdfbox.googlecode.com/svn/trunk/