Merge PDF Documents using .NET PDF Library
Essential® PDF supports merging multiple PDF documents from disk and stream.
Check the following video to quickly get started with merge PDF document in .NET using the PDF Library.
Merging multiple documents from disk and stream
You can merge the multiple PDF document using Merge method of PdfDocumentBase class, by specifying the path of the documents in a string array.
Refer to the following code example to merge multiple documents from disk.
using Syncfusion.Pdf;
//Creates a new PDF document.
PdfDocument finalDoc = new PdfDocument();
//Creates a string array of source files to be merged.
string[] source = { "file1.pdf", "file2.pdf" };
//Merges PDFDocument.
PdfDocument.Merge(finalDoc, source);
//Saves the final document.
finalDoc.Save("Sample.pdf");
//Closes the document.
finalDoc.Close(true);
using Syncfusion.Pdf;
//Creates a new PDF document.
PdfDocument finalDoc = new PdfDocument();
//Creates a string array of source files to be merged.
string[] source = { "file1.pdf", "file2.pdf" };
//Merges PDFDocument.
PdfDocument.Merge(finalDoc, source);
//Saves the final document.
finalDoc.Save("Sample.pdf");
//Closes the document.
finalDoc.Close(true);
Imports Syncfusion.Pdf
'Creates a new PDF document.
Dim finalDoc As New PdfDocument()
'Creates a string array of source files to be merged.
Dim source As String() = {"file1.pdf", "file2.pdf"}
'Merges PDFDocument.
PdfDocument.Merge(finalDoc, source)
'Saves the final document.
finalDoc.Save("Sample.pdf")
'Closes the document.
finalDoc.Close(True)
You can merge the PDF document streams by using the following code example.
using Syncfusion.Pdf;
//Creates a PDF document.
PdfDocument finalDoc = new PdfDocument();
Stream stream1 = File.OpenRead("file1.pdf");
Stream stream2 = File.OpenRead("file2.pdf");
//Creates a PDF stream for merging.
Stream[] streams = { stream1, stream2 };
//Merges PDFDocument.
PdfDocumentBase.Merge(finalDoc, streams);
//Saves the document.
finalDoc.Save("sample.pdf");
//Closes the document.
finalDoc.Close(true);
//Disposes the streams.
stream1.Dispose();
stream2.Dispose();
using Syncfusion.Pdf;
//Creates a PDF document.
PdfDocument finalDoc = new PdfDocument();
Stream stream1 = File.OpenRead("file1.pdf");
Stream stream2 = File.OpenRead("file2.pdf");
//Creates a PDF stream for merging.
Stream[] streams = { stream1, stream2 };
//Merges PDFDocument.
PdfDocumentBase.Merge(finalDoc, streams);
//Saves the document.
finalDoc.Save("sample.pdf");
//Closes the document.
finalDoc.Close(true);
//Disposes the streams.
stream1.Dispose();
stream2.Dispose();
Imports Syncfusion.Pdf
'Creates a PDF document.
Dim finalDoc As New PdfDocument()
Dim stream1 As Stream = File.OpenRead("file1.pdf")
Dim stream2 As Stream = File.OpenRead("file2.pdf")
'Creates a PDF stream for merging.
Dim streams As Stream() = {stream1, stream2}
'Merges PDFDocument.
PdfDocumentBase.Merge(finalDoc, streams)
'Saves the document.
finalDoc.Save("sample.pdf")
'Closes the document.
finalDoc.Close(True)
'Disposes the streams.
stream1.Dispose()
stream2.Dispose()
You can download a complete working sample from GitHub.
Importing pages from multiple documents
Essential® PDF provides support for importing the pages from one document to another document using ImportPage method. The following code illustrates this. The imported page is added to the end of the original document.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Load the PDF document.
PdfLoadedDocument lDoc = new PdfLoadedDocument("Input.pdf");
//Create a new document.
PdfDocument document = new PdfDocument();
//Imports the page at 1 from the lDoc.
document.ImportPage(lDoc, 1);
//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Closes the document.
document.Close(true);
lDoc.Close(true);
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Load the PDF document.
PdfLoadedDocument lDoc = new PdfLoadedDocument("file1.pdf");
//Create a new document.
PdfDocument document = new PdfDocument();
//Imports the page at 1 from the lDoc.
document.ImportPage(lDoc, 1);
//Save the document.
document.Save("sample.pdf");
//Close the document.
document.Close(true);
lDoc.Close(true)
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
'Load the PDF document
Dim lDoc As New PdfLoadedDocument("file1.pdf")
'Creates a new document
Dim document As New PdfDocument()
'Imports the page at 1 from the lDoc
document.ImportPage(lDoc, 1)
'Save the document
document.Save("sample.pdf")
'Close the document
document.Close(True)
lDoc.Close(True)
You can download a complete working sample from GitHub.
You can import multiple pages from an existing document by using ImportPageRange method. The following code example illustrates this.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Load the PDF document.
PdfLoadedDocument lDoc = new PdfLoadedDocument("Input.pdf");
//Create a new document.
PdfDocument document = new PdfDocument();
//Imports the page at 1 from the lDoc.
document.ImportPageRange(lDoc, 0, lDoc.Pages.Count - 1);
//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Closes the document.
document.Close(true);
lDoc.Close(true);
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Loads PDF document.
PdfLoadedDocument lDoc = new PdfLoadedDocument("file1.pdf");
//Create a new document.
PdfDocument document = new PdfDocument();
//Imports the pages from the lDoc.
document.ImportPageRange(lDoc, 0, lDoc.Pages.Count - 1);
//Save the document.
document.Save("sample.pdf");
//Closes the documents.
document.Close(true);
lDoc.Close(true);
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
'Loads PDF document
Dim lDoc As New PdfLoadedDocument("file1.pdf")
'Creates a new document
Dim document As New PdfDocument()
'Imports the pages from the lDoc
document.ImportPageRange(lDoc, 0, lDoc.Pages.Count - 1)
'Saves the document
document.Save("sample.pdf")
'Closes the documents
document.Close(True)
lDoc.Close(True)
You can download a complete working sample from GitHub.
You can also import pages from multiple documents and arrange the pages by using ImportPage method. The following code example explains the same.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Loads document.
PdfLoadedDocument lDoc = new PdfLoadedDocument("file1.pdf");
//Loads document.
PdfLoadedDocument lDoc2 = new PdfLoadedDocument("file1.pdf");
//Create the new document.
PdfDocument document = new PdfDocument();
//Imports and arranges the pages.
document.ImportPage(lDoc, 2);
document.ImportPage(lDoc2, 1);
//Saves the document.
document.Save("sample.pdf");
//Closes the documents.
document.Close(true);
lDoc.Close(true);
lDoc2.Close(true);
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Loads document.
PdfLoadedDocument lDoc = new PdfLoadedDocument("file1.pdf");
//Loads document.
PdfLoadedDocument lDoc2 = new PdfLoadedDocument("file1.pdf");
//Create the new document.
PdfDocument document = new PdfDocument();
//Imports and arranges the pages.
document.ImportPage(lDoc, 2);
document.ImportPage(lDoc2, 1);
//Saves the document.
document.Save("sample.pdf");
//Closes the documents.
document.Close(true);
lDoc.Close(true);
lDoc2.Close(true);
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
'Loads a document
Dim lDoc As New PdfLoadedDocument("file1.pdf")
Dim lDoc2 As New PdfLoadedDocument("file2.pdf")
'create a new document
Dim document As New PdfDocument()
'imports and arranges the pages
document.ImportPage(lDoc, 2)
document.ImportPage(lDoc2, 1)
'Saves the document
document.Save("sample.pdf")
'Closes the documents
document.Close(True)
lDoc.Close(True)
lDoc2.Close(True)
You can download a complete working sample from GitHub.
Best practices
Merging multiple large PDF documents can lead to high runtime memory. So, you can split the documents into multiple documents and later you can merge. This method avoids the extensive memory usage and increases the performance.
NOTE
Note: The parent PDF document has all the contents in run time memory. It releases the memory once the final PDF document instance is disposed.
You can split a large PDF document into multiple documents using Split method of PdfLoadedDocument class. The following code snippet explains this.
//PDF doesn't support splitting the document into multiple documents C#.NET Cross platforms.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
//Loads the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("large.pdf");
//Splits the document.
loadedDocument.Split("split.pdf");
//Close the document.
loadedDocument.Close(true);
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
'Loads the PDF document
Dim loadedDocument As New PdfLoadedDocument("large.pdf")
'Splits the document
loadedDocument.Split("split.pdf")
'Close the document
loadedDocument.Close(True)
You can download a complete working sample from GitHub.
The following code shows how to merge multiple PDF documents using Merge method.
using Syncfusion.Pdf;
//Input documents.
string[] inputDocuments = Directory.GetFiles("../../Data/Split");
//Creates a PDF document.
PdfDocument document = new PdfDocument();
//Merges the document.
PdfDocumentBase.Merge(document, inputDocuments);
//Save and close the document.
document.Save("Output.pdf");
document.Close(true);
using Syncfusion.Pdf;
//Input documents.
string[] inputDocuments = Directory.GetFiles("../../Data/Split");
//Creates a PDF document.
PdfDocument document = new PdfDocument();
//Merges the document.
PdfDocumentBase.Merge(document, inputDocuments);
//Save and close the document.
document.Save("Output.pdf");
document.Close(true);
Imports Syncfusion.Pdf
'Input documents
Dim inputDocuments As String() = Directory.GetFiles("../../Data/Split")
'Create a PDF document
Dim document As New PdfDocument()
'Merges the document
PdfDocumentBase.Merge(document, inputDocuments)
'Save and close the document
document.Save("Output.pdf")
document.Close(True)
You can download a complete working sample from GitHub.
Optimizing PDF resources when merging PDF documents
Essential® PDF provides support to optimize the PDF resources when merging PDF documents. You can optimize the resources by enabling the OptimizeResources property available in the PdfMergeOptions class.
Refer to the following code example to optimize the PDF resources when merging PDF documents.
using Syncfusion.Pdf;
//Create a new PDF document.
PdfDocument finalDoc = new PdfDocument();
//Creates a string array of source files to be merged.
string[] source = { "file1.pdf", "file2.pdf" };
PdfMergeOptions mergeOptions = new PdfMergeOptions();
//Enable Optimize Resources.
mergeOptions.OptimizeResources = true;
//Merges PDFDocument.
PdfDocument.Merge(finalDoc, mergeOptions, source);
//Save the final document.
finalDoc.Save("Sample.pdf");
//Close the document.
finalDoc.Close(true);
using Syncfusion.Pdf;
//Create a new PDF document.
PdfDocument finalDoc = new PdfDocument();
//Creates a string array of source files to be merged.
string[] source = { "file1.pdf", "file2.pdf" };
PdfMergeOptions mergeOptions = new PdfMergeOptions();
//Enable Optimize Resources.
mergeOptions.OptimizeResources = true;
//Merges PDFDocument.
PdfDocument.Merge(finalDoc, mergeOptions, source);
//Save the final document.
finalDoc.Save("Sample.pdf");
//Close the document.
finalDoc.Close(true);
Imports Syncfusion.Pdf
'Create a new PDF document
Dim finalDoc As New PdfDocument()
'Creates a string array of source files to be merged
Dim source As String() = {"file1.pdf", "file2.pdf"}
Dim mergeOptions As New PdfMergeOptions()
'Enable Optimize Resources
mergeOptions.OptimizeResources = True
'Merges PDFDocument
PdfDocument.Merge(finalDoc, mergeOptions, source)
'Save the final document
finalDoc.Save("Sample.pdf")
'Close the document
finalDoc.Close(True)
You can download a complete working sample from GitHub.
Extend the margin of the PDF pages while merging PDF document
The Syncfusion® PDF library provides support for extending the margins of the pdf pages by using the ExtendMargin property available from the PdfMergeOptions class. When ExtendMargin is set to true, then a specified margin is considered while merging the existing documents.
The following code sample illustrates this.
using Syncfusion.Pdf;
//Create a new PDF document.
PdfDocument finalDoc = new PdfDocument();
//Create new instance for the document margin.
PdfMargins margin = new PdfMargins();
margin.All = 40;
//Set margin.
finalDoc.PageSettings.Margins = margin;
//Create a string array of source files to be merged.
string[] source = { "file1.pdf", "file2.pdf" };
PdfMergeOptions mergeOptions = new PdfMergeOptions();
// Enable the Extend Margin Property.
mergeOptions.ExtendMargin=true;
//Merge PDFDocument.
PdfDocument.Merge(finalDoc, mergeOptions, source);
//Save the final document.
finalDoc.Save("Sample.pdf");
//Close the document.
finalDoc.Close(true);
using Syncfusion.Pdf;
//Create a new PDF document.
PdfDocument finalDoc = new PdfDocument();
//Create new instance for the document margin.
PdfMargins margin = new PdfMargins();
margin.All = 40;
//Set margin.
finalDoc.PageSettings.Margins = margin;
//Create a string array of source files to be merged.
string[] source = { "file1.pdf", "file2.pdf" };
PdfMergeOptions mergeOptions = new PdfMergeOptions();
// Enable the Extend Margin Property.
mergeOptions.ExtendMargin=true;
//Merge PDFDocument.
PdfDocument.Merge(finalDoc, mergeOptions, source);
//Save the final document.
finalDoc.Save("Sample.pdf");
//Close the document.
finalDoc.Close(true);
Imports Syncfusion.Pdf
'Create a new PDF document
Dim finalDoc As New PdfDocument()
'Create new instance for the document margin
Dim margin As PdfMargins = New PdfMargins()
margin.All = 40
'Set margin
finalDoc.PageSettings.Margins = margin
'Create a string array of source files to be merged
Dim source As String() = {"file1.pdf", "file2.pdf"}
Dim mergeOptions As New PdfMergeOptions()
'Enable the Extend Margin Property
mergeOptions.ExtendMargin=true
'Merge PDFDocument
PdfDocument.Merge(finalDoc, mergeOptions, source)
'Save the final document
finalDoc.Save("Sample.pdf")
'Close the document
finalDoc.Close(True)
You can download a complete working sample from GitHub.
Merge PDF document without compromising accessibility tags
The Syncfusion® PDF library enables users to merge PDF documents while maintaining their accessibility standards, which are essential for users relying on screen readers and other assistive technologies. By enabling the MergeAccessibilityTags
property in the PdfMergeOptions class, the tagged structure of the documents is preserved in the final merged PDF. The default value for this property is false.
Refer to the following code to merge PDF documents without losing accessibility tags.
using Syncfusion.Pdf;
//Create a new PDF document.
PdfDocument finalDoc = new PdfDocument();
//Create a string array of source files to be merged.
string[] source = { "file1.pdf", "file2.pdf" };
PdfMergeOptions mergeOptions = new PdfMergeOptions();
//Enable the Merge Accessibility Tags.
mergeOptions.MergeAccessibilityTags = true;
//Merge PDFDocument.
PdfDocument.Merge(finalDoc, mergeOptions, source);
//Save the final document.
finalDoc.Save("Sample.pdf");
//Close the document.
finalDoc.Close(true);
using Syncfusion.Pdf;
//Create a new PDF document.
PdfDocument finalDoc = new PdfDocument();
//Create a string array of source files to be merged.
string[] source = { "file1.pdf", "file2.pdf" };
PdfMergeOptions mergeOptions = new PdfMergeOptions();
//Enable the Merge Accessibility Tags.
mergeOptions.MergeAccessibilityTags = true;
//Merge PDFDocument.
PdfDocument.Merge(finalDoc, mergeOptions, source);
//Save the final document.
finalDoc.Save("Sample.pdf");
//Close the document.
finalDoc.Close(true);
Imports Syncfusion.Pdf
'Create a new PDF document
Dim finalDoc As New PdfDocument()
'Create a string array of source files to be merged
Dim source As String() = {"file1.pdf", "file2.pdf"}
Dim mergeOptions As New PdfMergeOptions()
'Enable the Merge Accessibility Tags.
mergeOptions.MergeAccessibilityTags = True
'Merge PDFDocument
PdfDocument.Merge(finalDoc, mergeOptions, source)
'Save the final document
finalDoc.Save("Sample.pdf")
'Close the document
finalDoc.Close(True)
You can download a complete working sample from GitHub.