Merge PDFs In C#


 In this article, we will create an application that will merge two pdf files into one using C# and .NET.

Merging multiple PDF files into one PDF file can be useful in a variety of situations. For example, you can keep similar documents (such as resumes) in a single PDF file and share a single file rather than many. To be able to achieve this and similar objectives, this article will teach you how to merge multiple PDF files in C#. PDF files are the most dependable file type for storing and saving data. There are numerous tools to merge PDF documents into a single pdf file.

In this article we are using IronPDF for Joining 2 or more PDFs. IronPDF is a .NET pdf library that helps us to create and edit PDF files programmatically. It supports .NET 6, .Net 5, .Net Core, .Net Standard and .Net Framework to handle all PDF-related tasks such as creating PDF files, combine PDF files into one PDF file, converting word documents to PDF documents, and deleting files.

This library makes it easy for developers to manipulate PDF pages and achieve their various objectives. IronPDF can be used as c merge pdf tool. If you're wondering how it's done, here's a simple example using Visual Studio.

Merging two or more PDF documents

Using C# and .NET, we will create an application that will merge two PDF files into one pdf document.

Step 1 - Create a Project

After launching Visual Studio, we must create an ASP.NET MVC, Console App or .Net core project. Select "File - New - Project" to get started.

Step 2 - Select a Console application

A new dialog will appear after selecting a project. Select "Console Application" and give your project a name and location. Then, press the "Ok" button. This will create your application. .

Step 3 - Install the IronPDF Library

After creating application, the first thing we are going to do is to install IronPDF so that we can import IronPDF in our application. In order to use its function for creating pdf document from html, merge pdf documents into single pdf documents, etc.

IronPDF supports .NET 6, .Net 5, .Net Core, .Net Standard and .Net Framework. Here we are going to install it using Package manager console and we can also install it using NuGet package.

You can find nuget package by clicking here. And you can installing IronPDF through Package manager console, Open the Package manager console and write the following command, and hit enter:

install-package ironpdf

It will install the IronPDF library in our project and will be available.

After successfully executing command in console then we get output in console as following below.

Step 4 - Writing code to merge PDF documents programmatically

After that installing Nuget package, you need to write the code snippet for merging two or more PDF documents into new pdf document. This code snippet uses method IronPdf.PdfDocument.Merge(pdfdoc_a, pdfdoc_b); to merge PDF files into single pdf. The parameters are passed according to the number of PDF files we need to merge as you can see in screenshot below.

You can copy the below code and paste it in your program.cs file.

 static void Main(string[] args)
            var html_a = @"<p> [PDF_A] </p>
                    <p> [PDF_A] 1st Page </p>
                    <div style = 'page-break-after: always;' ></div>
                    <p> [PDF_A] 2nd Page</p>";

            var html_b = @"<p> [PDF_B] </p>
                    <p> [PDF_B] 1st Page </p>
                    <div style = 'page-break-after: always;' ></div>
                    <p> [PDF_B] 2nd Page</p>";

            var Renderer = new IronPdf.ChromePdfRenderer();

            var pdfdoc_a = Renderer.RenderHtmlAsPdf(html_a);
            var pdfdoc_b = Renderer.RenderHtmlAsPdf(html_b);
            var merged = IronPdf.PdfDocument.Merge(pdfdoc_a, pdfdoc_b);


This code can be used in multiple scenarios:

Case 1: Adding C# PDF Cover Pages

Whether you've merged two PDF document into new pdf document or need to add a page, adding a cover page to your C# PDF document is easy thanks to IronPDF — it only takes two lines of code.

Then, using the "ChromePdfRenderer" class, we create our cover page and later use the "Merge()" function to embed the two PDFs and create a single new pdf document. Take a look at the following code snippet below and experiment with it in your project.

Case 2: Converting and merging two different HTML files

You can merge two HTML files using this same method by first using Renderer.RenderHtmlAsPdf(a);  to convert the HTML pages to PDF, and then IronPdf.PdfDocument.Merge(a, b); to merge the files. In our code sample we had merged two documents in pdf format in single document.

If we want to merged more than two files than as mentioned earlier we just have to pass more parameter according to number of files in IronPdf.PdfDocument.Merge(); we want to merged i.e. if we want three file then we have pass three parameter like IronPdf.PdfDocument.Merge(parameter1,parameter2,parameter3).

we had used "Merge()" to merge multiple pdf documents into pdf file but upto until now we had not saved that merged file anywhere. So now we are going to save that merged file into our folder by using "merge.SaveAs()".

As you can see in our sample code we are passing name in "merge.SaveAs()" method to save our merged file and giving it the name we had passed. You can pass file along with name to save the merged pdf file to a specific location.

If you want to explore more you can visit this link Merge PDF.



IronPDF will merge all the PDF files you have, no matter how many pages are contained within them. Please note that in order to use IronPDF, you must first obtain the library's license.

The tutorial is now complete. I hope you have understood everything, and if you have any questions, please leave them in the comments section.

You can get the project by clicking on this link. If you want to buy the entire suite of Iron Software products, our special offer currently allows you to buy five packages for the price of just two.

Similar Articles