PDF to image using Java [duplicate]
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?
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:
-
Load PDF or PS file (use PSDocument class for that):
PDFDocument document = new PDFDocument(); document.load(new File("input.pdf"));
-
Create the renderer
SimpleRenderer renderer = new SimpleRenderer(); // set resolution (in DPI) renderer.setResolution(300);
-
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 :)
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.