Get a page from pdf and save it to an image file with itext

There is a pdf file, and I want to import the 2nd page as an image and save it to a jpeg file. Is it possible and how to do it?

This is the code how I import a page:

Document document = new Document();
File file = File.createTempFile("", "");
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();

final int backPage = 2;
PdfReader reader = new PdfReader(pdf.getAbsolutePath());
PdfImportedPage importedPage = writer.getImportedPage(reader, backPage);
com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(importedPage);

Now I get an image instance, but I don’t know how to write it to a jpeg file.

Answer

Appearently (according to 1T3XT BVBA), you can only save an iText Image from a PDF page, not a raster image. You can store it everywhere, if you will use later to put it in another PDF page… otherwise, you’ll have to use a tool like JPedal:

http://www.idrsolutions.com/convert-pdf-to-images/

===================================

EDIT: maybe PDFBox can do it for you too!:

http://pdfbox.apache.org/commandlineutilities/PDFToImage.html

http://gal-levinsky.blogspot.it/2011/11/convert-pdf-to-image-via-pdfbox.html

Leave a Reply

Your email address will not be published. Required fields are marked *