Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF to image using Java [duplicate]

Tags:

java

image

pdf

jar

I to want convert PDF pages into an image (PNG,JPEG/JPG or GIF). I want them in full-page sizes.

How can this be done using Java? What libraries are available for achieving this?

like image 736
yohan.jayarathna Avatar asked Feb 03 '11 12:02

yohan.jayarathna


2 Answers

In Ghost4J library (http://ghost4j.sourceforge.net), since version 0.4.0 you can use a SimpleRenderer to do the job with few lines of code:

  1. Load PDF or PS file (use PSDocument class for that):

        PDFDocument document = new PDFDocument();     document.load(new File("input.pdf")); 
  2. Create the renderer

        SimpleRenderer renderer = new SimpleRenderer();      // set resolution (in DPI)     renderer.setResolution(300); 
  3. Render

        List<Image> images = renderer.render(document); 

Then you can do what you want with your image objects, for example, you can write them as PNG like this:

            for (int i = 0; i < images.size(); i++) {                 ImageIO.write((RenderedImage) images.get(i), "png", new File((i + 1) + ".png"));             } 

Note: Ghost4J uses the native Ghostscript C API so you need to have a Ghostscript installed on your box.

I hope it will help you :)

like image 76
zippy1978 Avatar answered Oct 15 '22 03:10

zippy1978


Apache PDF Box can convert PDFs to jpg,bmp,wbmp,png, and gif.

The library even comes with a command line utility called PDFToImage to do this.

If you download the source code and look at the PDFToImage class you should be able to figure out how to use PDF Box to convert PDFs to images from your own Java code.

like image 25
Dónal Boyle Avatar answered Oct 15 '22 03:10

Dónal Boyle