简体   繁体   中英

How can I join two PDF's using iTextSharp?

I tried to do it using this tutorial as a base, but it's throwing a null reference exception at the line specified below. Should I be doing this a different way? If not, why would it throw an null reference exception (both page and cb are NOT null). Code:

        string filePath = @"c:\temp\test_new.pdf";
        string attachPath = @"c:\temp\test.pdf";

        Console.WriteLine("Begin!");
        Document d = new Document();

        if(File.Exists(filePath)){File.Delete(filePath);}

        FileStream fs = new FileStream(filePath, FileMode.Create);

        PdfWriter pw = PdfWriter.GetInstance(d, fs);
        d.Open();
        d.Add(new Paragraph("New document!  Now lets add an attachment!"));

        PdfReader pRdr = new PdfReader(new FileStream(attachPath,FileMode.Open));
        PdfReaderContentParser parser = new PdfReaderContentParser(pRdr);

        MemoryStream ms = new MemoryStream();
        PdfWriter writer = PdfWriter.GetInstance(d, ms);
        writer.Open();
        PdfContentByte cb = writer.DirectContent;
        PdfImportedPage page;
        int rotation;
        d.SetPageSize(PageSize.LETTER);
        for (int i = 1; i <= pRdr.NumberOfPages; i++)
        {
            d.NewPage();
            page = writer.GetImportedPage(pRdr, i);
            rotation = pRdr.GetPageRotation(i);
            if (rotation == 90 || rotation == 270)
            {
                cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, pRdr.GetPageSizeWithRotation(i).Height);
            }
            else
            {
  /*NULL EXCEPTION HERE!!!*/cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);  //NULL EXCEPTION HERE!!!

            }
        }

1) Use PdfCopy not PdfWriter. PdfWriter is for writing generated PDFs from a Document. PdfCopy is made for copying pages from A to B.

2) If you're problem is the result of an exception PLEASE post the exception. It'll remove much of the guesswork you see in the comments.

3) PdfImportedPage is just that page's contents and resources. You lose annotations (form fields and the like), bookmarks, and so forth. PdfCopy can help with some of that, but not all.

OK. I might get blasted for not answering your question, but there is a simpler way to merge two PDFs: don't use iTextSharp, use iTextDotNet

I found a post on how to do it: http://alex.buayacorp.com/merge-pdf-files-with-itextdotnet-and-net.html

I remembered this because I had to do this a couple of years ago. It does work, and well.

Check out my simple embPDFUtils library, where you can configure the merging and splitting process through an XML-Configuration file. There are methods to concatenate pdf files, split them and create pdf files from images. It is free. Check it out here: http://blog.mecum.biz/2011/11/how-master-xml-and-mistress-xsd-helped-itextsharp-out-of-the-claws-of-hippi-o-cratic-chaos-huggermugger/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM