itext7 pdf to image [英] itext7 pdf to image
问题描述
我正在使用iText7(java),我正在寻找一种将pdf页面转换为图像的方法。
在较旧的iText版本中,你可以这样做:
I am using iText7(java) and am looking for a way to convert a pdf page to image. In older iText versions you could do this :
PdfImportedPage page = writer.getImportedPage(reader, 1);
Image image = Image.getInstance(page);
但iText7没有PdfImportedPage。
But iText7 does not have PdfImportedPage .
我的用例,我有一页pdf文件。我需要添加一个表并调整pdf内容的大小以适应单个页面。在旧的iText中,我会创建一个页面,添加表格,将现有的pdf页面转换为图像,调整图像大小并将调整后的图像添加到新页面。在iText7中有没有新的方法。
My use case, I have a one page pdf file. I need to add a table and resize the pdf contents to fit a single page. In old iText I would create a page , add table, convert existing pdf page to image, resize image and add that resized image to new page. Is there a new way to do this in iText7.
感谢Bruno的回答我使用以下代码:
Thanks to Bruno's answer I got this working with following code :
PdfPage origPage = readerDoc.getPage(1);
Rectangle rect = origPage.getPageSize();
Document document = new Document(writerDoc);
Table wrapperTable = new Table(1);
Table containerTable = new Table(new float[]{0.5f,0.5f});
containerTable.setWidthPercent(100);
containerTable.addCell( "col1");
containerTable.addCell("col2");
PdfFormXObject pageCopy = origPage.copyAsFormXObject(writerDoc);
Image image = new Image(pageCopy);
image.setBorder(Border.NO_BORDER);
image.setAutoScale(true);
image.setHeight(rect.getHeight()-250);
wrapperTable.addCell(new Cell().add(containerTable).setBorder(Border.NO_BORDER));
wrapperTable.addCell(new Cell().add(image).setBorder(Border.NO_BORDER));
document.add(wrapperTable);
document.close();
readerDoc.close();
推荐答案
请阅读iText 7的官方文档,更具体地说第6章:重用现有内容PDF文档
Please read the official documentation for iText 7, more specifically Chapter 6: Reusing existing PDF documents
在PDF中,有 Form XObject 的概念。 Form XObject 是一段PDF内容,存储在页面的内容流之外,因此XObject代表eXternal Object。在 Form XObject 中使用 Form 这个词可能会令人困惑,因为人们可能会想到一个带有字段的可填写表单的表单。为避免这种混淆,我们在iText 5中引入了术语 PdfTemplate
。
In PDF, there's the concept of Form XObjects. A Form XObject is a piece of PDF content that is stored outside the content stream of a page, hence XObject which stands for eXternal Object. The use of the word Form in Form XObject could be confusing, because people might be thinking of a form as in a fillable form with fields. To avoid that confusing, we introduced the term PdfTemplate
in iText 5.
类 PdfImportedPage
你引用的是 PdfTemplate
的子类:它是一段可以在另一页中重用的PDF语法。多年来,我们注意到人们也对 PdfTemplate
这个词感到困惑。
The class PdfImportedPage
you refer to was a subclass of PdfTemplate
: it was a piece of PDF syntax that could be reused in another page. Over the years, we noticed that people also got confused by the word PdfTemplate
.
在iText 7中,我们返回基础知识。在谈论Form XObject时,我们使用类 PdfFormXObject
。在谈论PDF文件中的页面时,我们使用类 PdfPage
。
In iText 7, we returned to the basics. When talking about a Form XObject, we use the class PdfFormXObject
. When talking about a page in a PDF file, we use the class PdfPage
.
这就是我们如何得到一个来自现有文档的 PdfPage
:
This is how we get a PdfPage
from an existing document:
PdfDocument origPdf = new PdfDocument(new PdfReader(src));
PdfPage origPage = origPdf.getPage(1);
这就是我们在新文档中使用该页面的方式:
This is how we use that page in a new document:
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdf);
如果你想使用 pageCopy
as一个图像
,只需像这样创建它:
If you want to use that pageCopy
as an Image
, just create it like this:
Image image = new Image(pageCopy);
这篇关于itext7 pdf to image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!