简体   繁体   English

itextpdf:合并pdf文件

[英]itextpdf : merge pdf files

Is it possible with itextpdf to merge two pdf files like this 是否可以使用itextpdf合并两个这样的pdf文件

  1. pdf-file-1 : 1 page with 3 lines of text pdf-file-1 :1页,每行3行

  2. pdf-file-2 : 1 page with 9 lines of text pdf-file-2 :1页,每行9行

results : 结果:

pdf-file-3 : 1 page with 12 lines text, merging results of pdf-file-1 and pdf-file-2 pdf-file-3 :1页,每行12行,合并pdf-file-1pdf-file-2

Is it possible with itextpdf to merge two pdf files like this... 是否可以使用itextpdf合并两个这样的pdf文件...

As Bruno explained in his comment, PDFs in general hardly know anything about the text they display, they don't know about chapters, sections, paragraphs, columns, etc. They don't even necessarily know about lines of text; 正如Bruno在他的评论中所解释的那样,PDF总体上几乎不了解它们显示的文本,它们不了解章,节,段落,列等。它们甚至不必知道文本的行; what we perceive as a line may be a loose collection of small chunks of text drawn at some abstract coordinates. 我们认为是线条的可能是在某些抽象坐标处绘制的一小部分文本的松散集合。

Thus, your question as is may not even make sense seen from the inside of the PDFs in question. 因此,从相关PDF的内部看,您的问题可能甚至没有意义。

If you re-formulate it, though, some similar question does make sense, eg 但是,如果重新制定公式,那么类似的问题确实有意义,例如

Is it possible with iTextPdf to merge two pdf files like this iTextPdf是否可以像这样合并两个pdf文件

  1. pdf-file-1: 1 page with content which only fills a small part of the page, eg only the top half pdf-file-1:1个页面,其内容仅占页面的一小部分,例如仅上半部分

  2. pdf-file-2: 1 page also with content which only fills a small part of the page, eg only the top half pdf-file-2:1页也包含仅占页面一小部分内容的内容,例如仅上半部分

results : 结果:

pdf-file-3: 1 page with the content of pdf-file-1 at the top and that of pdf-file-2 right below it, merging results of pdf-file-1 and pdf-file-2 pdf-file-3:1页,顶部为pdf-file-1内容,而其右下为pdf-file-2内容,合并pdf-file-1和pdf-file-2的结果

This indeed is possible, especially it is possible using iText. 这确实是可能的,尤其是使用iText是可能的。 While this functionality is not an explicit feature of iText, iText does offer a low-level API which fairly easily allows to implement such merging features, eg: 尽管此功能不是iText的显式功能,但iText确实提供了一个低级API,可以很容易地实现这些合并功能,例如:

  • the PdfDenseMergeTool presented in this answer does exactly what is described above, it puts the contents of as many source pages on a target page as fit there; 此答案中呈现的PdfDenseMergeTool确实执行上述操作,它将目标页面上尽可能多的源页面的内容放置在该页面中; and
  • the PdfVeryDenseMergeTool presented in this answer does even more, it even splits source page contents to squeeze even more onto the target pages. 此答案中呈现的PdfVeryDenseMergeTool甚至可以做更多的事情,它甚至可以拆分源页面内容以将更多内容压缩到目标页面上。

Beware, though, those classes are not hardened by years of use and improvement, they are proofs of concept for which certain corner cases will still have to considered. 但是请注意,这些类并没有经过多年的使用和改进而得到加强,它们是概念的证明,对于某些特殊情况,仍然需要考虑。

In particular they only consider actual page content, not annotations or similar active content. 特别是,它们仅考虑实际的页面内容,而不考虑注释或类似的活动内容。 On the other hand they even consider white rectangles drawn onto the empty page as content. 另一方面,他们甚至将绘制在空白页面上的白色矩形视为内容。 PDFs in which each page initially is filled with white are considered completely full in spite of what actually is visible 尽管实际上可见,但最初在每个页面中都用白色填充的PDF仍被认为已完全填充

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

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