DEV Community

Ali Jago Seif
Ali Jago Seif

Posted on

Merge PDF files in C#

In this article, I am going to show you how to merge multiple PDF files programmatically using Merge_File method and easy PDF SDK. This C# sample program demonstrates how to merge PDF pages using the PDFProcessor API.

The file names are assumed to be in the array files[]. The merged file is saved under “outFileName”

private void Merge_File(string[] files, string outFileName)
{
    try
    {
        PDFProcessor oPDFProcessor = new PDFProcessor();
            // loop over all the files, which are concatenated one by one
            string inFileName = files[0];
    
        for(int i = 1; i < files.Length; i++)
        {
            oPDFProcessor.Merge(inFileName, files[i], outFileName);         
            inFileName = outFileName;
        }               
            
        MessageBox.Show(files.Length.ToString() + " files merged!");
    }
    catch(System.Runtime.InteropServices.COMException err)
    {
        MessageBox.Show(err.Message + " (" + err.ErrorCode.ToString() + ")");
    }
}

Top comments (1)

Collapse
 
atir_tahir profile image
Atir Tahir

This is quite helpful but what if we want one-stop solution for different file formats. Like if I want to merge Word files or Excel files using single API? At this point, GroupDocs.Merger for .NET API allows you to join two Word files or two Excel spreadsheet etc using same piece of code.
See how simple is this: