DEV Community

Cover image for How to Merge PDF Files in C# (Developer Tutorial)
Zeeshan Wazir
Zeeshan Wazir

Posted on

How to Merge PDF Files in C# (Developer Tutorial)

Merge multiple PDF files into a single, cohesive PDF document with IronPDF's easy-to-use functionality. By combining input PDF files, you can create a new PDF document that seamlessly integrates all the content from the original PDF documents. This makes organizing and managing multiple PDF documents a breeze.

Combining multiple PDF files into a single document is a common requirement in many applications. With IronPDF, a powerful .NET library, merging PDFs becomes a straightforward process. In this tutorial, we'll explore step-by-step how to merge PDFs in C# using IronPDF.

IronPDF - The C# PDF Library

IronPDF is a popular C# library used for creating, processing, and editing PDF documents within .NET applications. It's particularly useful for developers who need to work with PDF files programmatically. Whether you need to generate PDFs from scratch, work with existing PDFs, or convert HTML to PDF, IronPDF provides a comprehensive set of features to meet your requirements.

Image description

Here are some of its key features:

  • PDF Creation: IronPDF allows developers to create PDF documents from scratch. This includes adding text, images, tables, and other elements to generate PDFs dynamically.
  • HTML to PDF: One of its main features is the ability to convert HTML content (websites, HTML files, etc.) to PDF. This is useful for generating PDF reports from web applications or saving web pages as PDFs.
  • PDF Editing: IronPDF enables developers to manipulate existing PDF documents. This includes editing text, images, annotations, and other elements within a PDF.
  • PDF Forms: It supports the creation and filling of PDF forms. Developers can programmatically create form fields, populate them with data, and extract form data from filled forms.
  • PDF Security: IronPDF provides features for securing PDF documents, such as adding passwords, permissions (like printing or copying restrictions), and digital signatures.
  • PDF Rendering: Developers can render PDFs to images, such as JPEG or PNG, which can be useful for displaying PDF content in web applications or converting PDF pages to images.
  • PDF Conversion: Besides HTML to PDF, IronPDF supports various other conversions, such as PDF to Image, PDF to HTML, PDF to Text, and more.
  • Document Merging: It allows merging multiple PDF documents into a single PDF file, which is handy for combining reports, invoices, or other documents.
  • Cross-Platform: IronPDF can be used in both Windows and Linux environments, making it versatile for a wide range of development scenarios.
  • Ease of Use: It offers a straightforward API that is easy to integrate into .NET applications. Developers can work with PDFs using familiar C# methods and classes.

IronPDF is widely used in various industries where PDF manipulation and creation are required, such as document management systems, reporting tools, invoicing applications, and more. Its comprehensive set of features makes it a valuable tool for .NET developers working with PDF files.

Merging PDFs in C# with IronPDF

To merge PDF files into a single PDF document, follow the steps below:

Step 1: Setting Up the Project

Create a new C# console application using Visual Studio. Follow the steps below:

  1. Launch Visual Studio and click "Create a New Project".
  2. From templates, select "Console App" and click Next.
  3. Give your project a name and choose the location. Then click Next.
  4. From Additional Information, select the .NET Framework and click Create.

Step 2: Installing IronPDF

Before we begin, make sure you have IronPDF installed in your C# project. You can easily add it via Tools -> NuGet Package Manager Console:

Install-Package IronPdf

Alternatively, you can also install it from NuGet Package Manager for Solutions. Right-click the solution explorer and select NuGet Package Manager. In NuGet browse tab, search for IronPDF and click install:

Image description

Step 3: Create or Obtain PDFs to Merge

You'll need the PDF files you want to combine. For this tutorial, let's assume we have two existing PDFs, pdf_a.pdf and pdf_b.pdf, in the project directory. If you don't have existing PDFs, you can also create PDFs dynamically from various sources such as HTML, Word documents, or images using IronPDF.

pdf_a File:

Image description

pdf_b File:

Image description

Step 4: Writing the Code to Combine Multiple PDF Files

Now, let's move to the following code to merge the two PDFs, pdf_a.pdf and pdf_b.pdf, into a single document.

Code Snippet:

In this code sample, we are going to merge two PDF documents:

using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Load the PDFs you want to merge
        PdfDocument pdfA = PdfDocument.FromFile("pdf_a.pdf");
        PdfDocument pdfB = PdfDocument.FromFile("pdf_b.pdf");

        // Merging PDF files using Merge method
        PdfDocument mergedPdf = PdfDocument.Merge(pdfA, pdfB);

        // Save the merged PDF
        mergedPdf.SaveAs("Merged.pdf");

        Console.WriteLine("Merged PDF document Saved!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Loading PDFs: We use PdfDocument.FromFile to load the existing PDFs pdf_a.pdf and pdf_b.pdf into pdfA and pdfB variables, respectively.
  • Merging PDFs: The PdfDocument.Merge method is then used to merge pdfA and pdfB into a single merged Pdf document.
  • Saving the Merged PDF: Finally, the merged PDF is saved as Merged.pdf using the SaveAs method.

Step 5: Running the Application

Build and run the application. If everything is set up correctly, you should see the message "Merged PDF document Saved!" in the console output. Here is how the "Merged.pdf" looks:

Image description

Advanced Options

  • Merge Multiple PDFs: You can also merge more than two PDF documents by adding the file path of all the PDF files using a for loop. Then, use the merge method in it to combine the different PDF documents into a new, consolidated document.
  • Order and Pages: The order in which you pass the PDF documents to Merge determines the order of pages in the merged PDF. For example, PdfDocument.Merge(pdfA, pdfB) will place the pages of pdfA first followed by pdfB.

Example with HTML to PDF:

You can also merge PDFs generated from HTML content. Here's an example merging HTML-generated PDFs:

using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // HTML content for PDF A
        string htmlA = "<p>[PDF_A] 1st Page</p>";

        // HTML content for PDF B
        string htmlB = "<p>[PDF_B] 1st Page</p>";

        var renderer = new ChromePdfRenderer();

        PdfDocument pdfDocA = renderer.RenderHtmlAsPdf(htmlA);
        PdfDocument pdfDocB = renderer.RenderHtmlAsPdf(htmlB);

        PdfDocument mergedHtmlPdf = PdfDocument.Merge(pdfDocA, pdfDocB);
        mergedHtmlPdf.SaveAs("Merged_HTML.pdf");

        Console.WriteLine("HTML-generated PDFs merged successfully!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Image description

Conclusion

Merging PDFs in C# using IronPDF is a straightforward process. Whether you're combining existing PDF files or generating PDFs from HTML content, IronPDF's PdfDocument.Merge method simplifies the task. By following this tutorial, you've learned how to efficiently merge PDF documents in your C# applications, providing a convenient solution for document organization and management. IronPDF's flexibility and ease of use make it a valuable tool for handling PDF-related tasks in .NET applications.

Explore the vast capabilities of IronPDF for all your PDF needs! Begin with a free trial to experience effortless PDF creation, editing, and conversion. When you're ready for commercial projects, easily license IronPDF for limitless access to its powerful features. Find detailed guidance on getting started in the documentation page.

Top comments (2)

Collapse
 
hrantors22 profile image
hrantors22 • Edited

Thanks for the guide! To merge PDF files in C#, you can utilize libraries like iTextSharp or PDFSharp, which provide APIs for manipulating PDF documents programmatically. However, nowadays, there are specialized software tools available that simplify PDF operations even further. For example I can share an article softwaretestinghelp.com/how-to-com... where you can find pdf merger download possibilities.

Collapse
 
xeshan6981 profile image
Zeeshan Wazir

Thanks for sharing!