How to Merge PDF using C#
Merge or combine multiple PDF into single PDF in C#
Merging PDF in C# is not straightforward task without using 3rd party library. This article shows how to merge multiple PDF files into a single PDF document using Aspose.PDF for .NET. The example is written in C# but the API can be used in other .NET programming languages as well such as VB.NET. PDF files are merged such that the first one is joined at the end of the other document.
Merge PDF Files using C# and DOM
To concatenate two PDF files:
- Create two Document objects, each containing one of the input PDF files.
- Then call the PageCollection collection’s Add method for the Document object you want to add the other PDF file to.
- Pass the PageCollection collection of the second Document object to the first PageCollection collection’s Add method.
- Finally, save the output PDF file using the Save method.
The following code snippet shows how to concatenate PDF files.
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Pages(); // Open first document Document pdfDocument1 = new Document(dataDir + "Concat1.pdf"); // Open second document Document pdfDocument2 = new Document(dataDir + "Concat2.pdf"); // Add pages of second document to the first pdfDocument1.Pages.Add(pdfDocument2.Pages); dataDir = dataDir + "ConcatenatePdfFiles_out.pdf"; // Save concatenated output file pdfDocument1.Save(dataDir);
Aspose.PDF Merger is an online free web application that allows you to investigate how presentation merging functionality works.