Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a straight forward way to append one PDF doc to another using iTextSharp?

Tags:

c#

pdf

itextsharp

I've scoured the Web looking for examples on how to do this. I've found a few that seem to be a little more involved then they need to be. So my question is, using iTextSharp, is there a fairly concise way to append one PDF document to another one?

Optimally this would NOT involve a third file. Just open the first PDF doc, append the second PDF doc to the first and then close them both.

like image 550
Danno Avatar asked Feb 19 '09 20:02

Danno


3 Answers

I really may be missing something, but I did something much simpler. I concede this solution probably won't update bookmarks (as in the best answer here so far), but it works flawlessly for me. Since I was merging documents with fillable forms, I used PdfCopyFields instead of PdfCopy.

Here is the code (I've stripped all error handling to make the actual code more visible, add a try..finally to close opened resources if you plan on using the code):

    void MergePdfStreams(List<Stream> Source, Stream Dest)
    {
        PdfCopyFields copy = new PdfCopyFields(Dest);

        foreach (Stream source in Source)
        {
            PdfReader reader = new PdfReader(source);
            copy.AddDocument(reader);
        }

        copy.Close();
    }

You can pass any stream, be it a FileStream, a MemoryStream (useful when reading the PDF from databases, no need for temporary files, etc.)

Sample usage:

    void TestMergePdfStreams()
    {
        List<Stream> sources = new List<Stream>()
        {
            new FileStream("template1.pdf", FileMode.Open),
            new FileStream("template2.pdf", FileMode.Open),
            new MemoryStream((byte[])someDataRow["PDF_COLUMN_NAME"])
        };

        MergePdfStreams(sources, new FileStream("MergedOutput.pdf", FileMode.Create));
    }
like image 117
Loudenvier Avatar answered Nov 19 '22 08:11

Loudenvier


Ok, It's not straight forward, but it works and is surprisingly fast. (And it uses a 3rd file, no such thing as open and append.) I 'discovered' this in the docs/examples. Here's the code:

private void CombineMultiplePDFs( string[] fileNames, string outFile ) {
    int pageOffset = 0;
    ArrayList master = new ArrayList();
    int f = 0;

    Document document = null;
    PdfCopy writer = null;
    while ( f < fileNames.Length ) {
        // we create a reader for a certain document
        PdfReader reader = new PdfReader( fileNames[ f ] );
        reader.ConsolidateNamedDestinations();
        // we retrieve the total number of pages
        int n = reader.NumberOfPages;
        ArrayList bookmarks = SimpleBookmark.GetBookmark( reader );
        if ( bookmarks != null ) {
            if ( pageOffset != 0 ) {
                SimpleBookmark.ShiftPageNumbers( bookmarks, pageOffset, null );
            }
            master.AddRange( bookmarks );
        }
        pageOffset += n;

        if ( f == 0 ) {
            // step 1: creation of a document-object
            document = new Document( reader.GetPageSizeWithRotation( 1 ) );
            // step 2: we create a writer that listens to the document
            writer = new PdfCopy( document, new FileStream( outFile, FileMode.Create ) );
            // step 3: we open the document
            document.Open();
        }
        // step 4: we add content
        for ( int i = 0; i < n; ) {
            ++i;
            if ( writer != null ) {
                PdfImportedPage page = writer.GetImportedPage( reader, i );
                writer.AddPage( page );
            }
        }
        PRAcroForm form = reader.AcroForm;
        if ( form != null && writer != null ) {
            writer.CopyAcroForm( reader );
        }
        f++;
    }
    if ( master.Count > 0 && writer != null ) {
        writer.Outlines = master;
    }
    // step 5: we close the document
    if ( document != null ) {
        document.Close();
    }
}
like image 24
Danno Avatar answered Nov 19 '22 06:11

Danno


Yes. I've seen a class called PdfManipulation posted in an iText forum. Using that class would involve a third file though.

The class is originally in VB.Net. I downloaded it from a post on vbforums.com. Apparently though, it doesn't have the merge files function, so I wrote one based on the code in that class.

This was written on a machine without iTextSharp. This might have bugs. I'm not even sure if page numbers are 0-based or 1-based. But give it a shot.

public static void MergePdfFiles(IEnumerable<string> files, string output) {
    iTextSharp.text.Document doc;
    iTextSharp.text.pdf.PdfCopy pdfCpy;

    doc = new iTextSharp.text.Document();
    pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(output, System.IO.FileMode.Create));
    doc.Open();

    foreach (string file in files) {
        // initialize a reader
        iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(file);
        int pageCount = reader.NumberOfPages;

        // set page size for the documents
        doc.SetPageSize(reader.GetPageSizeWithRotation(1));

        for (int pageNum = 1; pageNum <= pageCount; pageNum++) {
            iTextSharp.text.pdf.PdfImportedPage page = pdfCpy.GetImportedPage(reader, pageNum);
            pdfCpy.AddPage(page);
        }

        reader.Close();
    }

    doc.Close();
}
like image 2
configurator Avatar answered Nov 19 '22 08:11

configurator