java 从pdf中获取页面并将其保存为带有itext的图像文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12935204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:50:37  来源:igfitidea点击:

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

javaimageimportitext

提问by Freewind

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?

有一个pdf文件,我想将第二页作为图像导入并将其保存为jpeg文件。是否有可能以及如何做到?

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 imageinstance, but I don't know how to write it to a jpeg file.

现在我得到了一个image实例,但我不知道如何将它写入 jpeg 文件。

采纳答案by Andrea Ligios

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:

显然(根据1T3XT BVBA),您只能从 PDF 页面保存 iText 图像,而不是光栅图像。你可以把它存储在任何地方,如果你以后要用它把它放在另一个 PDF 页面中......否则,你将不得不使用像 JPedal 这样的工具:

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

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

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

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

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

编辑:也许 PDFBox 也可以为您做!:

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

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

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

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

回答by mkl

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.

Image.getInstance(importedPage) 不会(正如人们可能认为的那样)将表示的页面呈现为某个位图,而只是创建一个包装对象,使导入的页面更容易添加到另一个 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.

iText 不是 PDF 渲染工具,尤其不是旧的 com.lowagie 变体。您可能想查看不同的产品,例如JPedal