Merging multiple PDF files into a single file can facilitate document management, distribution, and printing. After merging, you only need to work with one file instead of multiple files. This article will explain how to merge multiple PDF files into a single PDF in C# and VB.NET.
This article includes the following topics:
- Merge Multiple PDF Files into a Single PDF
- Merge Specific Pages of Multiple PDF Files into a Single PDF
- Merge PDF Files with Different Page Sizes into One PDF with the Same Page Size
- Merge PDF Files from Streams
Installation
In order to merge PDF files, this article uses a third-party library called Spire.PDF for .NET.
You can install Spire.PDF for .NET via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console, and then executing the following command:
PM> Install-Package Spire.PDF
Alternatively, you can also download the API from the official website and extract the package, then add the DLL files under the BIN folder to your project as references.
Merge Multiple PDF Files into a Single PDF in C# and VB.NET
Spire.PDF offers a static method – PdfDocument.MergeFiles(string[] inputFiles) which allows you to merge multiple PDF files specified by the paths in a string array to a single PDF. The following are the detailed steps:
- Put the PDF files’ paths into a string array.
- Merge the PDF files specified by the paths in the string array into a single PDF using PdfDocument.MergeFiles(string[] inputFiles) method.
- Save the merged PDF to file using PdfDocumentBase.Save(string fileName) method.
C#
using Spire.Pdf;
namespace MergePdfFiles
{
internal class Program
{
static void Main(string[] args)
{
//Put the PDF files' paths into a string array
string[] files = new string[] { "File1.pdf", "File2.pdf", "File3.pdf" };
//Merge the PDF files specified by the paths into a single PDF
PdfDocumentBase pdf = PdfDocument.MergeFiles(files);
//Save the result PDF file
pdf.Save("Merge.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Namespace MergePdfFiles
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Put the PDF files' paths into a string array
Dim files = New String() {"File1.pdf", "File2.pdf", "File3.pdf"}
'Merge the PDF files specified by the paths into a single PDF
Dim pdf As PdfDocumentBase = PdfDocument.MergeFiles(files)
'Save the result PDF file
pdf.Save("Merge.pdf")
End Sub
End Class
End Namespace
Merge Specific Pages of Multiple PDF Files into a Single PDF in C# and VB.NET
You can merge a specific page or a range of pages of multiple PDF files into a single PDF file using PdfDocument.InsertPage(PdfDocument ldDoc, int pageIndex) or PdfDocument.insertPageRange(PdfDocument ldDoc, int startIndex, int endIndex) method. The following are the detailed steps:
- Initialize an instance of PdfDocument class.
- Load the first PDF file using PdfDocument.LoadFromFile(string fileName) method.
- Initialize an instance of PdfDocument class.
- Load the second PDF file using PdfDocument.LoadFromFile(string fileName) method.
- Initialize an instance of PdfDocument class to create a new PDF file.
- Import a specific page of the first PDF to the new PDF using PdfDocument.InsertPage(PdfDocument ldDoc, int pageIndex) method.
- Import a range of pages of the second PDF to the new PDF using PdfDocument.insertPageRange(PdfDocument ldDoc, int startIndex, int endIndex) method.
- Save the new PDF to file using PdfDocument.SaveToFile(string fileName) method.
C#
using Spire.Pdf;
namespace MergePdfPages
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of PdfDocument class
PdfDocument pdf1 = new PdfDocument();
//Load the first PDF file
pdf1.LoadFromFile("File1.pdf");
//Initialize an instance of PdfDocument class
PdfDocument pdf2 = new PdfDocument();
//Load the second PDF file
pdf1.LoadFromFile("File2.pdf");
//Initialize an instance of PdfDocument class to create a new PDF file
PdfDocument newPdf = new PdfDocument();
//Import the 1st page of the first PDF to the new PDF
newPdf.InsertPage(pdf1, 0);
//Import the 3rd and the 4th pages of the second PDF to the new PDF
newPdf.InsertPageRange(pdf2, 2, 3);
//Save the result PDF file
newPdf.SaveToFile("MergePages.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Namespace MergePdfPages
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Initialize an instance of PdfDocument class
Dim pdf1 As PdfDocument = New PdfDocument()
'Load the first PDF file
pdf1.LoadFromFile("File1.pdf")
'Initialize an instance of PdfDocument class
Dim pdf2 As PdfDocument = New PdfDocument()
'Load the second PDF file
pdf1.LoadFromFile("File2.pdf")
'Initialize an instance of PdfDocument class to create a new PDF file
Dim newPdf As PdfDocument = New PdfDocument()
'Import the 1st page of the first PDF to the new PDF
newPdf.InsertPage(pdf1, 0)
'Import the 3rd and the 4th pages of the second PDF to the new PDF
newPdf.InsertPageRange(pdf2, 2, 3)
'Save the result PDF file
newPdf.SaveToFile("MergePages.pdf")
End Sub
End Class
End Namespace
Merge PDF Files with Different Page Sizes into One PDF with the Same Page Size in C# and VB.NET
You can merge PDF files with different page sizes into a single PDF with the same page size by creating a new PDF with a specific page size, then redrawing the page content of the original PDF files to the new PDF.
- Put the PDF files’ paths into a string array.
- Initialize an instance of PdfDocument class to create a new PDF file, set its page size and margins.
- Iterate through the string array.
- Initialize an instance of the PdfDocument class and load the PDF file specified by the path in the array using PdfDocument.LoadFromFile(string fileName) method.
- Iterate through all pages in the PDF file.
- Add pages to the new PDF using PdfDocument.Pages.Add() method.
- Initialize an instance of PdfTextLayout class and then specify the text layout as one page to ensure the page content of the loaded PDF fit the page size of the new PDF.
- Redraw the page content of the loaded PDF to the pages of the new PDF using PdfDocument.Pages[int].CreateTemplate().Draw(PdfPageBase page, PointF location, PdfTextLayout format) method.
- Save the new PDF to file using PdfDocument.SaveToFile(string fileName) method.
C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace MergePdfFiles
{
internal class Program
{
static void Main(string[] args)
{
//Put the PDF files' paths into a string array
string[] files = new string[] { "File1.pdf", "File2.pdf", "File3.pdf" };
//Initialize an instance of PdfDocument class to create a new PDF file
PdfDocument newPdf = new PdfDocument();
//Set page size
newPdf.PageSettings.Size = PdfPageSize.A4;
//Remove page margins
newPdf.PageSettings.Margins.All = 0;
//Loop through the string array
for (int i = 0; i < files.Length - 1; i++)
{
//Load the PDF file
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(files[i]);
//Iterate through all pages in the PDF
for (int j = 0; j < pdf.Pages.Count; j++)
{
//Add a page to the new PDF
PdfPageBase newPage = newPdf.Pages.Add();
//Specify text layout as 1 page to ensure the page content fit the page size of the new PDF
PdfTextLayout layout = new PdfTextLayout();
layout.Layout = PdfLayoutType.OnePage;
//Redraw the page content of the loaded PDF to the page of the new PDF
pdf.Pages[j].CreateTemplate().Draw(newPage, new System.Drawing.PointF(0, 0), layout);
}
}
//Save the result PDF file
newPdf.SaveToFile("MergePdfsWithDifferentPageSizes.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Namespace MergePdfFiles
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Put the PDF files' paths into a string array
Dim files = New String() {"File1.pdf", "File2.pdf", "File3.pdf"}
'Initialize an instance of PdfDocument class to create a new PDF file
Dim newPdf As PdfDocument = New PdfDocument()
'Set page size
newPdf.PageSettings.Size = PdfPageSize.A4
'Remove page margins
newPdf.PageSettings.Margins.All = 0
'Loop through the string array
For i = 0 To files.Length - 1 - 1
'Load the PDF file
Dim pdf As PdfDocument = New PdfDocument()
pdf.LoadFromFile(files(i))
'Iterate through all pages in the PDF
For j As Integer = 0 To pdf.Pages.Count - 1
'Add a page to the new PDF
Dim newPage As PdfPageBase = newPdf.Pages.Add()
'Specify text layout as 1 page to ensure the page content fit the page size of the new PDF
Dim layout As PdfTextLayout = New PdfTextLayout()
layout.Layout = PdfLayoutType.OnePage
'Redraw the page content of the loaded PDF to the page of the new PDF
pdf.Pages(j).CreateTemplate().Draw(newPage, New Drawing.PointF(0, 0), layout)
Next
Next
'Save the result PDF file
newPdf.SaveToFile("MergePdfsWithDifferentPageSizes.pdf")
End Sub
End Class
End Namespace
Merge PDF Files from Streams in C# and VB.NET
To merge multiple PDF files from streams, you can use the PdfDocument.MergeFiles(Stream[] streams) method. The following are the detailed steps:
- Read the PDF files into streams using File.OpenRead(string path) method.
- Put the streams into a stream array.
- Merge the PDF streams using PdfDocument.MergeFiles(Stream[] streams) method.
- Save the result PDF to file using PdfDocumentBase.Save(string fileName) method. You can also save the PDF to stream by passing a Stream object to the PdfDocumentBase.Save() method.
C#
using Spire.Pdf;
namespace MergePdfFilesFromStreams
{
internal class Program
{
static void Main(string[] args)
{
//Read the PDF files into streams
Stream stream1 = File.OpenRead("File1.pdf");
Stream stream2 = File.OpenRead("File2.pdf");
Stream stream3 = File.OpenRead("File3.pdf");
//Put the streams into a stream array
Stream[] streams = { stream1, stream2, stream3};
//Merge the pdf streams
PdfDocumentBase pdf = PdfDocument.MergeFiles(streams);
//Save the result PDF file
pdf.Save("MergePdfsFromStreams.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Namespace MergePdfFilesFromStreams
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Read the PDF files into streams
Dim stream1 As Stream = File.OpenRead("File1.pdf")
Dim stream2 As Stream = File.OpenRead("File2.pdf")
Dim stream3 As Stream = File.OpenRead("File3.pdf")
'Put the streams into a stream array
Dim streams = {stream1, stream2, stream3}
'Merge the pdf streams
Dim pdf As PdfDocumentBase = PdfDocument.MergeFiles(streams)
'Save the result PDF file
pdf.Save("MergePdfsFromStreams.pdf")
End Sub
End Class
End Namespace