How to merge pdf documents and add pages in between

807 Views Asked by At

I have a requirement where i have to merge multiple PDF documents and add pages also with some text. For e.g. I have copied pages from one PDF and now i have to add a page with some text and then i need to copy pages from second PDF and then again i need to add a page with some text...

I have tried merging PDFs but it just merge the PDFs i want to add some text after each PDF document.

I want to use iTextSharp. Below is the code snippet:

// step 1: creation of a document-object Document document = new Document();

        // step 2: we create a writer that listens to the document
        PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
        if (writer == null)
        {
            return;
        }

        // step 3: we open the document
        document.Open();

        foreach (string fileName in fileNames)
        {
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(fileName);
            reader.ConsolidateNamedDestinations();

            // step 4: we add content
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                PdfImportedPage page = writer.GetImportedPage(reader, i);
                writer.AddPage(page);
            }


            //This commented part is not working
            ////Add a new page to the pdf file
            //document.NewPage();

            //Paragraph paragraph = new Paragraph();
            //Font titleFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA
            //                          , 15
            //                          , iTextSharp.text.Font.BOLD
            //                          , BaseColor.BLACK
            //    );
            //Chunk titleChunk = new Chunk("Comments", titleFont);
            //paragraph.Add(titleChunk);
            //writer.Add(paragraph);

            //paragraph = new Paragraph();
            //Font textFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA
            //                         , 12
            //                         , iTextSharp.text.Font.NORMAL
            //                         , BaseColor.BLACK
            //    );
            //Chunk textChunk = new Chunk("Hello", textFont);
            //paragraph.Add(textChunk);
            //writer.Add(paragraph);
            //document.Add(paragraph);

            reader.Close();

        }

        // step 5: we close the document and writer
        writer.Close();
        document.Close();

Thanks in advance.

1

There are 1 best solutions below

0
On BEST ANSWER

You can't use document.newPage() in combination with PdfCopy. If you want to insert extra pages with content that is created on the fly, you need to create a new document in memory: Create PDF in memory instead of physical file

For instance, you could create this method:

private byte[] CreatePdf(String comments)
{
    Document doc = new Document(PageSize.LETTER);
    using (MemoryStream output = new MemoryStream())
    {
        PdfWriter wri = PdfWriter.GetInstance(doc, output);
        doc.Open();
        Paragraph header = new Paragraph("Comments");
        doc.Add(header);
        Paragraph paragraph = new Paragraph(comments);
        doc.Add(paragraph);
        doc.Close();
        return output.ToArray();
    }
}

In your code, you could use that method like this:

writer.AddDocument(new PdfReader(CreatePdf("Test comment")););

Note that you don't need to loop over the pages. You have:

for (int i = 1; i <= reader.NumberOfPages; i++)
{
     PdfImportedPage page = writer.GetImportedPage(reader, i);
     writer.AddPage(page);
}

You can use:

writer.AddDocument(reader);