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#

  • var renderer = new ChromePdfRenderer();
  • var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
  • var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);
  • var merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b);
  • 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 used 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:

List<PdfDocument> pdfs = new List<PdfDocument>()
{
 pdfdoc_a,
 pdfdoc_b,
 pdfdoc_c,
    // ...
};
var merged = PdfDocument.Merge(pdfs);
merged.SaveAs("Merged.pdf");
List<PdfDocument> pdfs = new List<PdfDocument>()
{
 pdfdoc_a,
 pdfdoc_b,
 pdfdoc_c,
    // ...
};
var merged = PdfDocument.Merge(pdfs);
merged.SaveAs("Merged.pdf");

Click here to view the How-to Guide, including examples, sample code, and files >