The Syncfusion UWP PDF library makes it easy to merge or combine multiple PDF files into a single PDF document in C#. The process is simple and efficient, and the merged PDF files retain their original formatting, layout, annotations, bookmarks, form fields, and appearance.
Here is an example of how to merge PDF files in C# using the Syncfusion .NET PDF library. You can merge multiple PDF files into a single PDF document with just a few lines of code.
//Create a new PDF document.
using (PdfDocument finalDocument = new PdfDocument())
{
// Open a stream from the first PDF file.
using (Stream firstPDFStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.File1.pdf"))
{
//Create a PdfLoadedDocument object from the stream.
using (PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(firstPDFStream))
{
//Open a stream from the second PDF file.
using (Stream secondPDFStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.File2.pdf"))
{
//Create a PdfLoadedDocument object from the stream.
using (PdfLoadedDocument loadedDocument2 = new PdfLoadedDocument(secondPDFStream))
{
//Merge the two PDF documents into the new PDF document.
PdfDocumentBase.Merge(finalDocument, loadedDocument1);
PdfDocumentBase.Merge(finalDocument, loadedDocument2);
//Create a memory stream to save the new PDF document.
MemoryStream outputStream = new MemoryStream();
//Save the new PDF document to the memory stream.
finalDocument.Save(outputStream);
//Save the stream as PDF document file in local machine.
Save(outputStream, "Sample.pdf");
}
}
}
}
}
Merging specific ranges of pages from different PDF documents is a quick and easy way to combine different parts of documents into a single PDF document.
//Create a new PDF document.
using (PdfDocument finalDocument = new PdfDocument())
{
// Open a stream from the first PDF file.
using (Stream firstPDFStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.File1.pdf"))
{
//Create a PdfLoadedDocument object from the stream.
using (PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(firstPDFStream))
{
//Import a range of pages from the first PDF document into the final document
//The range starts at page 2 and ends at the last page
finalDocument.ImportPageRange(loadedDocument1, 1, loadedDocument1.Pages.Count - 1);
//Open a stream from the second PDF file.
using (Stream secondPDFStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.File2.pdf"))
{
//Create a PdfLoadedDocument object from the stream.
using (PdfLoadedDocument loadedDocument2 = new PdfLoadedDocument(secondPDFStream))
{
//Import a range of pages from the second PDF document into the final document.
//The range starts at page 2 and ends at page 5
finalDocument.ImportPageRange(loadedDocument2, 2, 5);
//Create a memory stream to save the new PDF document.
MemoryStream outputStream = new MemoryStream();
//Save the new PDF document to the memory stream.
finalDocument.Save(outputStream);
//Save the stream as PDF document file in local machine.
Save(outputStream, "Sample.pdf");
}
}
}
}
}
When merging multiple PDF files, users can create bookmarks based on the titles of individual files. This will help quickly navigate to specific sections of the merged file.
//Create a new PDF document.
using (PdfDocument finalDocument = new PdfDocument())
{
// Open a stream from the first PDF file.
using (Stream firstPDFStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.File1.pdf"))
{
//Create a PdfLoadedDocument object from the stream.
using (PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(firstPDFStream))
{
//Open a stream from the second PDF file.
using (Stream secondPDFStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.File2.pdf"))
{
//Create a PdfLoadedDocument object from the stream.
using (PdfLoadedDocument loadedDocument2 = new PdfLoadedDocument(secondPDFStream))
{
//Merge the two PDF documents into the new PDF document.
PdfDocumentBase.Merge(finalDocument, loadedDocument1);
PdfDocumentBase.Merge(finalDocument, loadedDocument2);
//Create a bookmark for the first PDF file
PdfBookmark bookmark1 = finalDocument.Bookmarks.Add("Chapter 1 - Barcodes");
//Set the destination page for the first bookmark
bookmark1.Destination = new PdfDestination(finalDocument.Pages[0]);
//Set the destination for the first bookmark
bookmark1.Destination.Location = new PointF(20, 20);
//Create a bookmark for the second PDF file
PdfBookmark bookmark2 = finalDocument.Bookmarks.Add("Chapter 2 - HTTP Succinctly");
//Set the destination page for the second bookmark
bookmark2.Destination = new PdfDestination(finalDocument.Pages[3]);
//Set the destination for the second bookmark
bookmark2.Destination.Location = new PointF(20, 20);
//Create a memory stream to save the new PDF document.
MemoryStream outputStream = new MemoryStream();
//Save the new PDF document to the memory stream.
finalDocument.Save(outputStream);
//Save the stream as PDF document file in local machine.
Save(outputStream, "Sample.pdf");
}
}
}
}
}
Merging PDF files using the Syncfusion.NET PDF library is a fast and efficient way to manage multiple PDF documents using C#. Besides this process, developers can also:
Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion is proud to hold the following industry awards.