0

Is it possible with itextpdf to merge two pdf files like this

  1. pdf-file-1: 1 page with 3 lines of text

  2. pdf-file-2: 1 page with 9 lines of text

results :

pdf-file-3: 1 page with 12 lines text, merging results of pdf-file-1 and pdf-file-2

Smittey
  • 2,475
  • 10
  • 28
  • 35
Valeriane
  • 936
  • 3
  • 16
  • 37
  • 2
    No, whoever is asking this, doesn't understand that the content of a PDF is added to a page using coordinates. You are mistaking PDF with a Word processing format. This isn't a limitation of iText. It's a limitation of PDF. – Bruno Lowagie Oct 08 '15 at 15:46
  • Ok, thank you. I didn't think about this – Valeriane Oct 08 '15 at 16:29
  • For an attempt to make it work nonetheless look at [this answer](http://stackoverflow.com/a/29078954/1729265). – mkl Oct 08 '15 at 18:31

1 Answers1

1

Is it possible with itextpdf to merge two pdf files like this...

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; 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.

If you re-formulate it, though, some similar question does make sense, e.g.

Is it possible with iTextPdf to merge two pdf files like this

  1. pdf-file-1: 1 page with content which only fills a small part of the page, e.g. only the top half

  2. pdf-file-2: 1 page also with content which only fills a small part of the page, e.g. only the top half

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

This indeed is possible, especially it is possible using 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, e.g.:

  • 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; and
  • the PdfVeryDenseMergeTool presented in this answer does even more, it even splits source page contents to squeeze even more onto the target pages.

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

Community
  • 1
  • 1
mkl
  • 90,588
  • 15
  • 125
  • 265