سؤال

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.

هل كانت مفيدة؟

المحلول

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 );
     }
}

نصائح أخرى

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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top