2
votes

I've found many solutions in here and in the 'iText in Action' book, to merge PDF's using the PDFCopy and PDFSmartCopy classes, but the only similar question asked I've seen, the guy worked it out himself but didn't post the answer. This post Add an existing PDF from file to an unwritten document using iTextSharp asks the same question but its at the end, so they suggest closing the existing document and then use PDFCopy, here I'd like to insert it anywhere. So here goes.

I'm creating an iTextSharp document with text and images using normal Sections, Phrases, Document and PDFWriter classes. This is code written over many years and works fine. Now we need to insert an existing PDF while creating this document as either a new Section or Chapter if that isn't possible. I have the PDF as a Byte array, so no problems getting a PDFReader. However, I cannot work out how to read that PDF and insert it into the existing document at the point I'm at. I can get access to the PDFWriter if need be, but for the rest of the document all access is via Sections. This is as far as I've got and I can add the PDFWriter as another parameter if necessary.

I've made some progress since the original post and amend the code accordingly.

    internal static void InsertPDF( Section section, Byte[] pdf )
    {
        this.document.NewPage();

        PdfReader pdfreader = new PdfReader( pdf );
        Int32 pages = pdfreader.NumberOfPages;
        for ( Int32 page = 1; page <= pages; page++ )
        {
            PdfImportedPage page = this.writer.GetImportedPage( planreader, pagenum );
            PdfContentByte pcb = this.writer.DirectContentUnder;
            pcb.AddTemplate( page, 0, 0 );
            this.document.NewPage();
        }
    }

It is close to doing what I want, but as I obviously don't understand the full workings of iText wonder if this is the correct way or there is a better way to do it.

If there is any other information I can provide, let me know.

Any pointers would be appreciated.

2
Are you talking about bringing in content from an existing PDF or bringing in pages. PDFs don't have "phrases" or "paragraphs" and although "sections" and "chapters" are ways of tagging content, unless you are actually generating a true tagged pdf this information gets lost when written out. One of iTextSharp's biggest strengths is that it tries to hide/abstract away the internals of a PDF as long as possible. The only thing that exists as both an abstraction and as a PDF object is a "page" which is why most code out there recommends importing a full page. - Chris Haas
I'm just trying to copy the existing PDF 'as is' as new pages in the document I'm in the process of creating. As you state I don't think the Section or Chapter is usable as that is for creating new content. I can understand how to add one at the start or end by copying the entire Stream of both, but inserting in the middle has me stumped. I can get access to the PDFWriter which I believe is what I need but I don't understand how to add the content to that and not lose the PDF I'm building up in the document. Thanks for commenting. - Graybo
I made some progress so amended the question. - Graybo
What is the question? Seems like you already successfully inserted the existing PDF. Are you now saying you want the inserted pages to turn up in the Outline tree? - Bruno Lowagie
I wonder if this is the correct way or there is a better way to do it. - If you really want to integrate that other PDF into the new one, using the PdfWriter method GetImportedPage is ok. For general merging, PdfCopy should be used instead. It is close to doing what I want - as it merely is close, I assume it isn't exactly what you want. Thus, what is missing? - mkl

2 Answers

4
votes

Just adding a little more meat to the answer. The solution ended up being found by researching what methods worked with a PdfTemplate which is what a PdfImportedPage is derived from. I've added a little more to show how it interacts with the rest of the document being built up. I hope this helps someone else.

internal static void InsertPDF( PdfWriter writer, Document document, Section section, Byte[] pdf )
{
    Paragraph para = new Paragraph();
    // Add note to show blank page is intentional
    para.Add( new Phrase( "PDF follows on the next page.", <your font> ) );
    section.Add( para );
    // Need to update the document so we render this page.
    document.Add( section );

    PdfReader reader = new PdfReader( pdf );
    PdfContentByte pcb = writer.DirectContentUnder;
    Int32 pages = planreader.NumberOfPages;
    for ( Int32 pagenum = 1; pagenum <= pages; pagenum++ )
    {
        document.NewPage();
        PdfImportedPage page = writer.GetImportedPage( reader, pagenum );
        // Render their page in our document.
        pcb.AddTemplate( page, 0, 0 );
     }
}
0
votes

for insert existing pdf into new page, i've change order newpage

PdfImportedPage page2 = writer.GetImportedPage(pdf, 1);
                cb.AddTemplate(page2, 0, 0);
                document.NewPage();