简体   繁体   English

使用iTextSharp合并pdf文件的内容

[英]Merge content of pdf files using iTextSharp

Is it possible to place the content of one pdf file to a specific place in another one (eg between two paragraphs) and have the original content automatically shift downwards so it does not underlay the new one? 是否可以将一个pdf文件的内容放置到另一个文件中的特定位置(例如,在两段之间),并使原始内容自动向下移动,从而不会在新文件的基础上放下?

If not, is it at least possible to get a height of only the content part of a pdf page? 如果不是,是否至少有可能仅获得pdf页面内容部分的高度?

I have a number of templates with AcroForms, where one is the main template containing several pages and I need to stamp and insert the other files into it at specific places. 我有许多AcroForms模板,其中一个是包含数个页面的主模板,我需要在其他特定位置标记其他文件并将其插入其中。

I can do that using PdfWriter and PdfTemplate to assemble the output using absolute positioning, but that seems a bit ugly. 我可以使用PdfWriterPdfTemplate通过绝对定位来组装输出,但这看起来有点难看。

As you ask multiple questions here, I'll quote the respective question when answering. 当您在此处提出多个问题时,我将在回答时引用相应的问题。

Is it possible to place the content of one pdf file to a specific place in another one (eg between two paragraphs) and have the original content automatically shift downwards so it does not underlay the new one? 是否可以将一个pdf文件的内容放置到另一个文件中的特定位置(例如,在两段之间),并使原始内容自动向下移动,从而不会在新文件的基础上放下?

PDF is not a format you can (easily) reflow. PDF 不是您可以(轻松)重排的格式。 And while your question does not refer to the worst case of reflowing (this would be insertion of some text, eg a sentence, into an existing paragraph), it is already difficult. 虽然你的问题并不是指回流的最坏的情况下(这是一些文本插入,例如一个句子, 一个现有的段落),它已经是困难的。 When moving down the paragraphs, eg you wouldn't want any footer lines to also move down; 向下移动段落时,例如,您不希望任何页脚行也向下移动; unfortunately footer lines are not (necessarily) marked as footer lines. 不幸的是,页脚线未(必需)标记为页脚线。 You would want to move images belonging to the content but not background images; 您可能想移动属于内容的图像,而不是背景图像; unfortunately images are not marked as foreground or background images. 不幸的是,图像未标记为前景或背景图像。 Etc. pp. 等等

In essence: iText does not have convenience methods for such a paragraph insertion, and any library that has them, will every so often create undesirable results. 本质上:iText没有方便的方法来插入这样的段落,任何拥有它们的库都会经常产生不希望的结果。

If not, is it at least possible to get a height of only the content part of a pdf page? 如果不是,是否至少有可能仅获得pdf页面内容部分的高度?

What is the content part of a PDF page? PDF页面的内容部分是什么? Do you mean excluding headers and footers? 您是说排除页眉和页脚吗? As mentioned above, headers and footers are not marked as such and, therefore, cannot (dependably) be automatically recognized. 如上所述,页眉和页脚没有这样标记,因此不能(可靠地)自动识别。 Thus, no . 因此, 没有 Or do you mean the smallest rectangle all page content is in? 还是说所有页面内容都位于最小的矩形中? In that case have a look at the sample ShowTextMargins from chapter 15 of iText in Action — 2nd Edition . 在这种情况下,请查看《 iText in Action —第二版》第15章中的示例ShowTextMargins This sample looks for text but you can easily generalize it by extending the TextMarginFinder used there to not only react to renderText events but also to renderImage. 该示例查找文本,但是您可以通过扩展用于该TextMarginFinder轻松地将其概括,以不仅对renderText事件做出反应,而且还对renderImage做出反应。 I'm afraid, though, that clipping paths might be ignored. 但是,恐怕剪切路径可能会被忽略。

I have a number of templates with AcroForms, where one is the main template containing several pages and I need to stamp and insert the other files into it at specific places. 我有许多AcroForms模板,其中一个是包含数个页面的主模板,我需要在其他特定位置标记其他文件并将其插入其中。

You mention Acroforms. 您提到了Acroforms。 Form elements are not part of the PDF page content, they are associated to it as annotations. 表单元素不是 PDF页面内容的一部分,它们作为注释与之关联。 Annotations are easier to move around than page content, but if you have page content and annotations belonging to each other , you will have to move both in concert. 注释比页面内容更容易移动,但是,如果页面内容注释属于彼此 ,则必须同时移动两者。

I can do that using PdfWriter and PdfTemplate to assemble the output using absolute positioning, but that seems a bit ugly. 我可以使用PdfWriter和PdfTemplate通过绝对定位来组装输出,但这看起来有点难看。

In what way does that seem ugly? 这看起来很难看? Is the output ugly or the code producing it? 输出是丑陋的还是代码产生的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM