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

2019-05-06 18:31发布

问题:

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.

回答1:

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



回答2:

Image.getInstance(importedPage) does not (as one might assume) render the denoted page as some bitmap but merely creates a wrapper object to make the imported page easier to add to another PDF.

iText is not a PDF rendering tool, especially not the old com.lowagie variant. You may want to look at different products, e.g. JPedal.