Merge content of pdf files using iTextSharp

ε祈祈猫儿з 提交于 2019-12-08 10:45:01

问题


Is it possible to place the content of one pdf file to a specific place in another one (e.g. between two paragraphs) and have the original content automatically shift downwards so it does not underlay the new one?

If not, is it at least possible to get a height of only the content part of a pdf page?

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.

I can do that using PdfWriter and PdfTemplate to assemble the output using absolute positioning, but that seems a bit ugly.


回答1:


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 (e.g. between two paragraphs) and have the original content automatically shift downwards so it does not underlay the new one?

PDF is not a format you can (easily) reflow. And while your question does not refer to the worst case of reflowing (this would be insertion of some text, e.g. a sentence, into an existing paragraph), it is already difficult. When moving down the paragraphs, e.g. 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.

If not, is it at least possible to get a height of only the content part of a pdf page?

What is the content part of a PDF page? 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. 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. 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.

You mention Acroforms. Form elements are not part of the PDF page content, they are associated to it as annotations. 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.

In what way does that seem ugly? Is the output ugly or the code producing it?



来源:https://stackoverflow.com/questions/14336374/merge-content-of-pdf-files-using-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!