C# code tutorial

PDF Processing & Editing PDF Creating & Inserting PDF to Image PDF Digital Signature PDF Extracting PDF Protecting PDF Chart PDF Barcode

Popular .NET SDK


The easiest way to merge PDF Files. Combine multiple PDF or image files into a single PDF document.

XsPDF is a .NET library used to create and modify PDF documents programmatically from .NET languages like C#. It allows developers to merge two or more PDF files into a single PDF document.

The PDF files can come from SSRS, from some local repoarts, and your PC's disk. Combining a bunch of somewhat arbitrary documents into one, XsPDF make it simple and easy. What's more, batch PDF documents combine is also supported.

The following C# code is a quick and dirty example, but it was enough to get the job done.


void CombinePDFs()
{
    // Load several PDF files in
    using (PdfDocument oneDoc = PdfReader.Open("file1.pdf", PdfDocumentOpenMode.Import))
    using (PdfDocument twoDoc = PdfReader.Open("file2.pdf", PdfDocumentOpenMode.Import))
    using (PdfDocument outPdf = new PdfDocument())
    {
        // Copy all pages from each PDF document
        CopyPages(oneDoc, outPdf);
        CopyPages(twoDoc, outPdf);

        // Save and show the document
        outPdf.Save("Output.pdf");
        Process.Start("Output.pdf");
    }
}

void CopyPages(PdfDocument fromPDF, PdfDocument toPDF)
{
    for (int i = 0; i < fromPDF.PageCount; i++)
    {
        // Append and merge each PDF page to the new PDF document
        toPDF.AddPage(fromPDF.Pages[i]);
                                
        // You can aslo get current PDF page instance,
        // then make some editing on this page, such as add some text or annotation
        // The following is a short demo for adding a line in the page bottom:

        //PdfPage page = toPDF.AddPage(fromPDF.Pages[i]);
        //XGraphics g = XGraphics.FromPdfPage(page);
        //g.DrawLine(XPens.Red, 0, page.Height - 10, page.Width, page.Height - 10);
    }
}

More Excel tutorial