How to Merge Pdf Files in a Folder in C# and VB.NET in WinForms PDF?
The Syncfusion Essential® PDF is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.
This sample explains how to merge all PDF files in a folder into a single PDF using the Syncfusion® .NET PDF library.
Steps to merge all PDF files in a folder programmatically in C#:
- Create a new console application project.
-
You can merge two PDF documents by using the Merge method of PdfDocumentBase class.
Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
C#
// Merge PDF file
PdfDocumentBase.Merge(document, loadedDocument);
VB.NET
' Merge PDF file
PdfDocumentBase.Merge(document, loadedDocument)
- Include the following namespaces in the Program.cs file:
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using System.IO;
VB.NET
Imports System.IO Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing
- Include the following code snippet in the main method of the Program.cs file to merge all PDF files in a folder into a single PDF document.
C#
// Get the folder path into DirectoryInfo
DirectoryInfo directoryInfo = new DirectoryInfo("../../Data/");
// Get the PDF files in folder path into FileInfo
FileInfo[] files = directoryInfo.GetFiles("*.pdf");
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Set enable memory optimization as true
document.EnableMemoryOptimization = true;
foreach (FileInfo file in files)
{
// Load the PDF document
FileStream fileStream = new FileStream(file.FullName, FileMode.Open);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream);
// Merge PDF file
PdfDocumentBase.Merge(document, loadedDocument);
// Close the existing PDF document
loadedDocument.Close(true);
}
// Save the PDF document
document.Save("MergedPDF.pdf");
// Close the instance of PdfDocument
document.Close(true);
VB.NET
' Get the folder path into DirectoryInfo
Dim directoryInfo As DirectoryInfo = New DirectoryInfo("../../Data/")
' Get the PDF files in folder path into FileInfo
Dim files() As FileInfo = directoryInfo.GetFiles("*.pdf")
' Create a new PDF document
Dim document As PdfDocument = New PdfDocument
' Set enable memory optimization as true
document.EnableMemoryOptimization = True
For Each file As FileInfo In files
' Load the PDF document
Dim fileStream As FileStream = New FileStream(file.FullName, FileMode.Open)
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(fileStream)
' Merge PDF file
PdfDocumentBase.Merge(document, loadedDocument)
' Close the existing PDF document
loadedDocument.Close(True)
Next
' Save the PDF document
document.Save("MergedPDF.pdf")
' Close the instance of PdfDocument
document.Close(True)
A complete work sample to merge all PDF documents in a folder into single PDF document can be downloaded from MergePDF.zip.
Place the files of different file formats in the data folder of project and execute the program to see the merged PDF document created by merging all the PDF files in the folder.
Take a moment to peruse the documentation, where you will find other options like RTF to PDF conversion, XPS to PDF conversion, PDF to image conversion, HTML to MHTML, HTML to SVG, partial webpage to SVG, and more.
Click here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to convert word document to PDF, RTF to PDF, XPS to PDF, TIFF to PDF, PDF to Image, and excel document to PDF.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
I hope you enjoyed learning about how to merge all PDF files in a folder in C# and VB.NET.
You can refer to our WinForms PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!