There are many reasons merging PDFs may be necessary. For example, merging PDF files allows you to print a single file rather than queueing several documents for the printer, combining related files simplifies the process of managing and storing many documents by reducing the number of files to search through and organize. In this article, you will learn how to merge multiple PDF documents into one PDF document and how to combine the selected pages from different PDF documents into one PDF in C# and VB.NET by using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Merge Multiple PDFs into a Single PDF
Spire.PDF for .NET offers the PdfDocument.MergeFiles() method to merge multiple PDF documents into a single document. The detailed steps are as follows.
- Get the paths of the documents to be merged and store them in a string array.
- Call PdfDocument.MergeFiles() method to merge these files.
- Save the result to a PDF document using PdfDocumentBase.Save() method.
- C#
- VB.NET
using System; using Spire.Pdf; namespace MergePDFs { class Program { static void Main(string[] args) { //Get the paths of the documents to be merged String[] files = new String[] { "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-1.pdf", "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-2.pdf", "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-3.pdf"}; //Merge these documents and return an object of PdfDocumentBase PdfDocumentBase doc = PdfDocument.MergeFiles(files); //Save the result to a PDF file doc.Save("output.pdf", FileFormat.PDF); } } }
Merge the Selected Pages of Different PDFs into One PDF
Spire.PDF for .NET offers the PdfDocument.InsertPage() method and the PdfDocument.InsertPageRange() method to import a page or a page range from one PDF document to another. The following are the steps to combine the selected pages from different PDF documents into a new PDF document.
- Get the paths of the source documents and store them in a string array.
- Create an array of PdfDocument, and load each source document to a separate PdfDocument object.
- Create another PdfDocument object for generating a new document.
- Insert the selected page or page range of the source documents to the new document using PdfDocument.InsertPage() method and PdfDocument.InsertPageRange() method.
- Save the new document to a PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using System; using Spire.Pdf; namespace MergeSelectedPages { class Program { static void Main(string[] args) { //Get the paths of the documents to be merged String[] files = new String[] { "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-1.pdf", "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-2.pdf", "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-3.pdf"}; //Create an array of PdfDocument PdfDocument[] docs = new PdfDocument[files.Length]; //Loop through the documents for (int i = 0; i < files.Length; i++) { //Load a specific document docs[i] = new PdfDocument(files[i]); } //Create a PdfDocument object for generating a new PDF document PdfDocument doc = new PdfDocument(); //Insert the selected pages from different documents to the new document doc.InsertPage(docs[0], 0); doc.InsertPageRange(docs[1], 1,3); doc.InsertPage(docs[2], 0); //Save the document to a PDF file doc.SaveToFile("output.pdf"); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.