Merge Two or More PDFs in C#
IronPDF's merge feature allows you to combine multiple PDF documents into a single file seamlessly. Whether you're working with HTML-rendered PDFs or existing PDF files, IronPDF provides a simple API for merging. Leverage IronPDF's powerful merge features to effortlessly combine your PDFs while maintaining the structure and content integrity of each document.
The 5 Steps to Merging PDF Documents in C#
// Step 1: Create a new instance of ChromePdfRenderer
var renderer = new IronPdf.ChromePdfRenderer();
// Step 2: Render the first HTML string to a PDF document
var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
// Step 3: Render the second HTML string to a PDF document
var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);
// Step 4: Merge the two PDF documents into a single document
var merged = IronPdf.PdfDocument.Merge(pdfdoc_a, pdfdoc_b);
// Step 5: Save the merged PDF document to a file
merged.SaveAs("Merged.pdf");
// Step 1: Create a new instance of ChromePdfRenderer
var renderer = new IronPdf.ChromePdfRenderer();
// Step 2: Render the first HTML string to a PDF document
var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
// Step 3: Render the second HTML string to a PDF document
var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);
// Step 4: Merge the two PDF documents into a single document
var merged = IronPdf.PdfDocument.Merge(pdfdoc_a, pdfdoc_b);
// Step 5: Save the merged PDF document to a file
merged.SaveAs("Merged.pdf");
' Step 1: Create a new instance of ChromePdfRenderer
Dim renderer = New IronPdf.ChromePdfRenderer()
' Step 2: Render the first HTML string to a PDF document
Dim pdfdoc_a = renderer.RenderHtmlAsPdf(html_a)
' Step 3: Render the second HTML string to a PDF document
Dim pdfdoc_b = renderer.RenderHtmlAsPdf(html_b)
' Step 4: Merge the two PDF documents into a single document
Dim merged = IronPdf.PdfDocument.Merge(pdfdoc_a, pdfdoc_b)
' Step 5: Save the merged PDF document to a file
merged.SaveAs("Merged.pdf")
Before we can begin merging our PDF documents, we first need to create the HTML string that will be used to render the PDF documents in this example. These HTML strings are named html_a
and html_b
. Once these have been created, we can move on to rendering and merging the PDFs.
First, you will need to create a new ChromePdfRenderer
instance. This powerful renderer will be used to convert the HTML content we created to high-quality PDF documents. By using the RenderHtmlAsPdf
method, we can then render the HTML content into PDF documents.
Now, it is time to merge the PDF documents into a single PDF named Merged.pdf
. This is done through the Merge
method, which takes the two PDF documents passed to it and merges them using just one line of code. This makes merging documents an efficient and easy-to-implement task with IronPDF.
Finally, all that is left to do is save the merged PDF. From here, you can utilize IronPDF's various PDF tools to further edit and manipulate this PDF document.
Beyond the basic merging of two PDF documents, IronPDF is capable of merging any number of PDF documents. In our basic example, we demonstrated how you would merge two PDF documents by specifying both PDFs as separate arguments. To merge more than two documents, you would need to replace this two-argument method with a List overload, as seen here in a more advanced code example:
// Create a list of PDF documents to be merged
List<IronPdf.PdfDocument> pdfs = new List<IronPdf.PdfDocument>()
{
pdfdoc_a,
pdfdoc_b,
pdfdoc_c,
// ... add more PDF documents as needed
};
// Merge all PDF documents in the list into a single document
var merged = IronPdf.PdfDocument.Merge(pdfs);
// Save the merged PDF document to a file
merged.SaveAs("Merged.pdf");
// Create a list of PDF documents to be merged
List<IronPdf.PdfDocument> pdfs = new List<IronPdf.PdfDocument>()
{
pdfdoc_a,
pdfdoc_b,
pdfdoc_c,
// ... add more PDF documents as needed
};
// Merge all PDF documents in the list into a single document
var merged = IronPdf.PdfDocument.Merge(pdfs);
// Save the merged PDF document to a file
merged.SaveAs("Merged.pdf");
' Create a list of PDF documents to be merged
Dim pdfs As New List(Of IronPdf.PdfDocument)() From {pdfdoc_a, pdfdoc_b, pdfdoc_c}
' Merge all PDF documents in the list into a single document
Dim merged = IronPdf.PdfDocument.Merge(pdfs)
' Save the merged PDF document to a file
merged.SaveAs("Merged.pdf")
Click here to view the How-to Guide, including examples, sample code, and files >