Merge PDF pages into One PDF Document in Csharp(Pdf Sharp dll)

In my previous post i have explain how to splite pdf documents

Some times we have requirement to Merge PDF document into one PDF Documents.
In this Article i am explaining how to merge pdf document into one.
For this i used pdfSharp dll which is free available into internet.
i am describing this example step by step.

Step 1(Create new Project)
Open Visual Studio
Go to File–>New–>Project

Go to Window Form Application and Give the “Mergepdfdoc” name to the project.

a new Form is showing right Click on Window Form and select View Code.
Step 2
put your inputpdf1.pdf,inputpdf2.pdf……….(you can choose another name) into debug folder of your project.
output file will also created in debug folder of your project.
Step 3
dowonload PdfSharp.dll from internet.
now add the reference of this dll to your project.
Right click on your solution Explorer–> Add Reference –>Browse(browse the downloaded dll path)–> ok
now add the button control on your page.
Drag and Drop the Button Control from the Toolbar and name it “btnmergepdf”.
double click on button and generate the OnClick Event.
step 4>
Create a function for Merging the pdf documents

        private static void MergeMultiplePDFIntoSinglePDFDoc(string outputpdfFilePath, string[] pdfFilescoll)
        {
            //create a new pdf output document
            PdfDocument outputPDFDocument = new PdfDocument();
            //loop for reading input pdf files 
            foreach (string pdfFile in pdfFilescoll)
            {
                PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
                outputPDFDocument.Version = inputPDFDocument.Version;
                //loop for reading all pages of input files
                foreach (PdfPage page in inputPDFDocument.Pages)
                {
                    // adding the input pages into output pdf documents
                    outputPDFDocument.AddPage(page);
                }
            }
             // At last save the output document
            outputPDFDocument.Save(outputpdfFilePath);
        }

Step 5
call the MergeMultiplePDFIntoSinglePDFDoc function into onClick event of btnmergepdf

        private void btnmergepdf_Click(object sender, EventArgs e)
        {
         string outputpdffilename = "mynewpdf.pdf";
         string[] Collofinputddffiles = {"inputpdf1.pdf","inputpdf2.pdf","inputpdf3.pdf"};
         MergeMultiplePDFIntoSinglePDFDoc(outputpdffilename ,Collofinputddffiles );
        }
This entry was posted in Grid View. Bookmark the permalink.

Leave a comment