In document-centric workflows, combining multiple PDF files into a single document is a critical functionality in many .NET applications, ranging from enterprise document management systems to customer-facing invoicing platforms. While many tools exist for this PDF merging task, Spire.PDF for .NET stands out with its balance of simplicity, performance, and cost-effectiveness.

Visual guide for Merge PDFs in C#

This guide explores how to merge PDF in C# using Spire.PDF, covering basic merging to advanced techniques with practical code examples.

Why Programmatic PDF Merging Matters

In enterprise applications, PDF merging is crucial for:

  • Consolidating financial reports
  • Combining scanned document batches
  • Assembling legal documentation packages
  • Automated archiving systems

Spire.PDF for .NET stands out with:

  • ✅ Pure .NET solution (no Acrobat dependencies)
  • ✅ Cross-platform support (.NET framework, .NET Core, .NET 5+)
  • ✅ Flexible page manipulation capabilities

How to Merge PDFs in C#: Step-by-Step Guide

Step 1. Install Spire.PDF

Before diving into the C# code to combine PDF files, it’s necessary to install the .NET PDF library via NuGet Package Manager.

  • In Visual Studio, right-click your project in Solution Explorer
  • Select Manage NuGet Packages
  • Search for Spire.PDF and install

Or in Package Manager Console, run the following:

PM> Install-Package Spire.PDF

Step 2: Basic PDF Merging - C# / ASP.NET Sample

Spire.PDF provides a direct method PdfDocument.MergeFiles() method to merge multiple PDFs into a single file. The below C# code example defines three PDF file paths, merges them, and saves the result as a new PDF.

using Spire.Pdf;

namespace MergePDFs
{
    class Program
    {
        static void Main(string[] args)
        {
            // Specify the PDF files to be merged
            string[] files = new string[] {"sample0.pdf", "sample1.pdf", "sample2.pdf"};

            // Merge PDF files 
            PdfDocumentBase pdf = PdfDocument.MergeFiles(files);

            // Save the result file
            pdf.Save("MergePDF.pdf", FileFormat.PDF);
        }
    }
}

Result: Combine three PDF files (total of 7 pages) into one PDF file.

Merge multiple PDF files into one PDF

Practical Example: Merge Selected Pages from Different PDFs

Merging selected pages involves combining specific pages from multiple PDFs into a new PDF document. Here’s how to achieve the task:

  • Define the PDF files to be merged.
  • Load PDFs into an array:
    • Create an array of PdfDocument objects.
    • Loops through to load each PDF into the array.
  • Create a new PDF: Initializes a new PDF document to hold the merged pages.
  • Insert specific pages into the new PDF:
    • InsertPage(): Insert a specified page to the new PDF (Page index starts at 0).
    • InsertPageRange(): Insert a range of pages to the new PDF.
  • Save the merged PDF: Save the new document to a PDF file.

Code Example:

using Spire.Pdf;

namespace MergePDFs
{
    class Program
    {
        static void Main(string[] args)
        {
            // Specify the PDF files to be merged
            string[] files = new string[] {"sample0.pdf", "sample1.pdf", "sample2.pdf"};

            // Create an array of PdfDocument
            PdfDocument[] pdfs = new PdfDocument[files.Length];

            // Loop through each PDF file
            for (int i = 0; i < files.Length; i++)
            {
                pdfs[i] = new PdfDocument(files[i]);
            }

            // Create a new PdfDocument object
            PdfDocument newPDF = new PdfDocument();

            // Insert the selected pages from different PDFs to the new PDF file
            newPDF.InsertPageRange(pdfs[0], 1, 2);
            newPDF.InsertPage(pdfs[1], 0);
            newPDF.InsertPage(pdfs[2], 1);

            // Save the new PDF file
            newPDF.SaveToFile("SelectivePageMerging.pdf");
        }
    }
}

Result: Combine selected pages from three separate PDF files into a new PDF.

Merge specified pages from different PDFs

Memory Efficient Solution: Merge PDF Files using Streams

For stream-based merging, refer to the C# code below:

using System.IO;
using Spire.Pdf;

namespace MergePDFsByStream
{
    class Program
    {
        static void Main(string[] args)
        {
            // Specify the PDF files to be merged
            string[] pdfFiles = {
                "MergePdfsTemplate_1.pdf",
                "MergePdfsTemplate_2.pdf",
                "MergePdfsTemplate_3.pdf"
            };

            // Initialize a MemoryStream array
            MemoryStream[] ms = new MemoryStream[pdfFiles.Length];
            
            // Read all PDF files to the MemoryStream
            for (int i = 0; i < pdfFiles.Length; i++)
            {
                byte[] fileBytes = File.ReadAllBytes(pdfFiles[i]);
                ms[i] = new MemoryStream(fileBytes);
            }

            // Merge PDF files using streams
            PdfDocumentBase pdf = PdfDocument.MergeFiles(ms);

            // Save the merged PDF file
            pdf.Save("MergePDFByStream.pdf", FileFormat.PDF);
        }
    }
}

Pro Tip: Learn more stream-based PDF handling techniques via the article: Load and Save PDF Files in Streams Using C#

Conclusion

Spire.PDF simplifies PDF merging in C# with its intuitive API and robust feature set. Whether you need to combine entire documents or specific pages, this library provides a reliable solution. By following the steps outlined in this guide, you can efficiently merge PDFs in your .NET applications while maintaining high quality and performance.


FAQs

Q1: Is Spire.PDF free to use?

A: Spire.PDF offers a free Community Edition with limitations (max 10 pages per document). To evaluate the commercial version without any limitations, request a free trial license here.

Q2: Can I merge PDFs from different sources?

A: Yes. Spire.PDF supports merging PDFs from various sources:

  • Local Files: Use LoadFromFile() method.
  • Streams: Use LoadFromStream() method.
  • Base64: Convert Base64 to a byte array first, then use LoadFromBytes() method.
  • URLs: Download the PDF to a stream or file first, then load it.

Q3: Can I add page numbers during merging?

A: After merging, you can add page numbers by following this guide: Add Page Numbers to a PDF in C#.

Q4. Where can I get support for Spire.PDF?

A: Check below resources:

Compared with Excel files, text files are easier to read and take up less memory as they contain only plain text data without any formatting or complex structure. Therefore, in certain situations where simplicity and efficiency are required, converting Excel files to text files can be beneficial. This article will demonstrate how to programmatically convert Excel to TXT format using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Convert Excel to TXT in C# and VB.NET

Spire.XLS for .NET offers the Worksheet.SaveToFile(string fileName, string separator, Encoding encoding) method to convert a specified worksheet to a txt file. The following are the detailed steps.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specified worksheet by its index using Workbook.Worksheets[sheetIndex] property.
  • Convert the Excel worksheet to a TXT file using Worksheet.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
using System.Text;

namespace ExcelToTXT
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load a sample Excel file
            workbook.LoadFromFile("sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Save the worksheet as a txt file
            sheet.SaveToFile("ExceltoTxt.txt", " ", Encoding.UTF8);

        }
    }
}

C#/VB.NET: Convert Excel to Text (TXT)

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Spire.PDF is an easy-to-use and powerful .NET PDF library. It can do a lot of conversions, and one of them is converting PDF page to image. As to converting PDF page to image, it works conveniently and flexibly. It has 6 overloaded functions named SaveAsImage that can make sure you find one meeting your need.

You can use Spire.PDF to convert any specific page of PDF document to BMP and Metafile image. Check it here.

In this article, we will discuss conversion with specified resolution.

[C#]
public Image SaveAsImage(int pageIndex, int dpiX, int dpiY)
  • pageIndex: specify which page to convert, 0 indicates the first page.
  • dpiX: specify the resolution of x coordinate axis in PDF page when converting.
  • dpiX: specify the resolution of y coordinate axis in PDF page when converting.
[C#]
Image image = documemt.SaveAsImage(0, PdfImageType.Bitmap, false, 400, 400)

In the sample code, the size of PDF page is Width = 612.0, Height = 792.0. We set the resolution to 400, 400. And we will get an image with width = 3400, height = 4400.

Here is sample code:

[C#]
PdfDocument documemt = new PdfDocument();
documemt.LoadFromFile(@"..\..\EnglishText.pdf");
Image image = documemt.SaveAsImage(0, PdfImageType.Bitmap, false, 400, 400);
image.Save(@"..\..\result.jpg");
documemt.Close();

Effect Screentshot:

image with specified resolution

page 275