Wednesday, April 20, 2011

Merging two or more PDF files to single PDF using ITextSharp dll

In this article i have explained to how merge PDF Files into a single with the help of ITextSharp dll. The ITextsharp dll is free ware, using this dll we can create a PDF programmatically.

please refer this link http://itextsharp.com/ to know about ITextSharp.

Before we use the ITextsharp class  we need to add the reference of this dll.


using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

private void Form1_Load(object sender, EventArgs e)
        {

            string des = @"C:\Users\Nagarajan\Desktop\test.pdf";

            string[] source = new string[2];

            source[0] = @"C:\Users\Nagarajan\Desktop\0396448 - Copy.pdf";

            source[1] = @"C:\Users\Nagarajan\Desktop\TERMS OF SALE - Copy.pdf";

            MergeFiles(des, source);

        }



/// <summary>
        /// Merge the PDF Files to one single file
        /// </summary>
        /// <param name="destinationFile">Destination File</param>
        /// <param name="sourceFiles">Files need to Merge</param>
        public static void MergeFiles(string destinationFile, string[] sourceFiles)
        {

            try
            {

                int f = 0;

                // we create a reader for a certain document

                PdfReader reader = new PdfReader(sourceFiles[f]);

                // we retrieve the total number of pages

                int n = reader.NumberOfPages;

                Console.WriteLine("There are " + n + " pages in the original file.");

                // step 1: creation of a document-object

                Document document = new Document(reader.GetPageSizeWithRotation(1));

                // step 2: we create a writer that listens to the document

                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));

                // step 3: we open the document

                document.Open();

                PdfContentByte cb = writer.DirectContent;

                PdfImportedPage page;

                int rotation;

                // step 4: we add content

                while (f < sourceFiles.Length)
                {

                    int i = 0;

                    while (i < n)
                    {

                        i++;

                        document.SetPageSize(reader.GetPageSizeWithRotation(i));

                        document.NewPage();

                        page = writer.GetImportedPage(reader, i);

                        rotation = reader.GetPageRotation(i);

                        if (rotation == 90 || rotation == 270)
                        {

                            cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);

                        }

                        else
                        {

                            cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);

                        }

                        Console.WriteLine("Processed page " + i);

                    }

                    f++;

                    if (f < sourceFiles.Length)
                    {

                        reader = new PdfReader(sourceFiles[f]);

                        // we retrieve the total number of pages

                        n = reader.NumberOfPages;

                        Console.WriteLine("There are " + n + " pages in the original file.");

                    }

                }

                // step 5: we close the document

                document.Close();

            }

            catch (Exception e)
            {

                Console.Error.WriteLine(e.Message);

                Console.Error.WriteLine(e.StackTrace);

            }

        }

 Thanks for reading this article

4 comments: