0

I have two PDFs. One is the main PDF and the other has an image that I need to insert into the first. Also in the second PDF, after inserting that image, I need to concatenate the remainder of the second PDF.

tshepang
  • 12,111
  • 21
  • 91
  • 136
tom
  • 2,190
  • 1
  • 23
  • 27
  • 1
    [This answer](http://stackoverflow.com/questions/23261198/how-to-move-text-written-in-type-3-font-from-one-pdf-to-other-pdf/23489682#23489682) might show the way. – mkl May 19 '14 at 21:00

2 Answers2

0

The solution was to superimpose the PDF page with the image onto the main PDF. Then concatenate the rest of it. "design_section" is the PDF with the image in it. This code will do:

    PdfReader confirmation_section = new PdfReader(SOURCE);
    PdfReader design_section = new PdfReader(SOURCE2);

    PdfStamper stamper = new PdfStamper(confirmation_section, new FileOutputStream(RESULT));
    PdfImportedPage page = stamper.getImportedPage(design_section, 1);
    int c = confirmation_section.getNumberOfPages();
    PdfContentByte background;
    for (int i = 1; i <= c; i++) {
        background = stamper.getUnderContent(i);
        if(i == c)
            background.addTemplate(page, 0, 0);
    }


    int d = design_section.getNumberOfPages();

    if(d > 1)  {
        for(int f = 2; f <= d; f++) {
            stamper.insertPage(c + f, confirmation_section.getPageSize(1));
            page = stamper.getImportedPage(design_section, f);
            stamper.getOverContent(c + f - 1).addTemplate(page, 0, 0);
            System.out.println("here we are in the loop c + f is: " + (c + f));
        }
    }

    stamper.close();

Pointed suggestion for iText -- how about renaming "addTemplate()" to "addPage()"???. iText is the most cryptic lib I have used and that includes regexp

tom
  • 2,190
  • 1
  • 23
  • 27
  • 1
    *how about renaming "addTemplate()" to "addPage()"* - imported pages are merely very special templates, and any template can be added using `addTemplate`. Thus, renaming as you propose would be inappropriate. – mkl May 23 '14 at 07:22
  • A template has a special meaning. It means something like an interface or abstract class. It is the general working framework for your to build on. In my mind, I did nothing with "templates" above... Look at pdf 995 (edit), there is very, very clear user interface. iText should use these terms for their classes. Terms like "Combine Existing Pdfs" "Extract PDF Pages". I know )), iText is a library, this is only meant for inspiration )). If iText is happy as it is, OK )). – tom May 24 '14 at 07:59
  • Then there is PdfCopy PDFStamper and PDFReader/Writer. Do we really need all three? Doen't something need to get depricated here in terms of the architectural direction of the library? I ended up interating over all three several times before I got something to work. Each iteration was a dead end where there was some hidden limitation of a given PDF-merge routine, then I would try the next one.... it seems silly.... – tom May 24 '14 at 08:06
  • Each of those tools has its own purpose. If you read the overview table in chapter 6 of my book, you can easily pick the class you need: http://manning.com/lowagie2/samplechapter6.pdf – Bruno Lowagie Jun 06 '14 at 15:41
0

Thanks for the follow up. I did read that many, many times ))) well, honestly, at least 6 times. I know it is just an excerpt and I am sure that there is more valuable information in the book, but with that said, I did not find what I was looking for. Where in that text does it discuss, compare and differentiate PdfCopy PDFStamper and PDFReader/Writer in the context of, for example, adding pages from one PDF to another?

tom
  • 2,190
  • 1
  • 23
  • 27