合并iText中的两个模板 [英] Merging two templates in iText

查看:543
本文介绍了合并iText中的两个模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个用Adobe Acrobat创建的PDF模板,它们都是单页的8.5x11文档。第一个模板(A.pdf)包含页面上半部分的内容。第二个模板(B.pdf)包含页面下半部分的内容。 (事实恰恰相反,两个模板中的内容并没有相互重叠。)

Let's say I have two PDF templates created with Adobe Acrobat, which are both single-page, 8.5x11 documents. The first template (A.pdf) has content for the top half of the page. The second template (B.pdf) has content for the bottom half of the page. (It just so happens the content in both templates does not "overlap" each other.)

我想使用iText来获取这两个模板并创建一个,来自它的合并模板(C.pdf)只是一个页面(上半部分是A.pdf的内容,下半部分是B.pdf的内容)。

I would like to use iText to take these two templates and create a single, "merged" template from it (C.pdf) that is only a single page (with A.pdf's content on the top half and B.pdf's content on the bottom half).

(我不想将这两个文件合并成2页文件。我需要最终产品为单页。)

(I do not want to "merge" these two files into a 2-page document. I need the final product to be a single page.)

I将在servlet环境中运行iText(Tomcat 6),但我不认为这会对答案产生影响。

I will be running iText in a servlet environment (Tomcat 6) but I don't think that makes a difference to the answer.

这可能吗?

推荐答案

我从Mark Storer那里得到了 iText邮件列表。解决方案是为每个文件获取 PdfTemplate 对象,然后使用 addTemplate()方法将它们添加到一起,例如:

I got help from Mark Storer on the iText mailing list. The solution is to get PdfTemplate objects for each file, and then use the addTemplate() method to add them together, e.g.:

PdfTemplate topOfPage = writer.getImportedPage( reader, 1 );
PdfTemplate bottomOfPage = writer.getImportedPage( reader, 2 );

PdfContentByte content = writer.getDirectContent();

// in PDF, "0, 0" is the lower left corner.
content.addTemplate( bottomOfPage );
content.addTemplate( topOfPage, 0, bottomOfPage.getHeight() );

这篇关于合并iText中的两个模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆