Articles in this section
Category / Section

How to merge PDF documents from ArrayList using C# and VB.NET

2 mins read

The Syncfusion Essential PDF is a feature-rich and high-performance .NET PDF library that is used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can merge PDF documents from ArrayList using C# and VB.NET.

Steps to merge PDF documents from ArrayList using C# programmatically

1. Create a new C# console application project.

Create Console application

2. Install the Syncfusion.PDF.WinForms NuGet packages as a reference to your .NET Framework application from NuGet.org.

Install NuGet packages

3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing

 

4. The following code example shows how to merge PDF documents from ArrayList using C#.

C#

// Get files from folder
string[] filePaths = Directory.GetFiles("../../Data/");
// Create the array list 
ArrayList mergearray = new ArrayList(filePaths.Length);
 
// Copy file path to the array list
for (var i = 0; i <= filePaths.Count() - 1; i++)
   mergearray.Add(filePaths[i]);
 
// Create a new PDF document 
PdfDocument finalDoc = new PdfDocument();
// Merge the PDF document 
for (int i = 0; i <= mergearray.Count - 1; i++)
{
   // Load an existing PDF 
   PdfLoadedDocument loadedDocument = new PdfLoadedDocument(mergearray[i].ToString());
   // Merge the PDF documnet 
   PdfDocument.Merge(finalDoc, loadedDocument);
}
// Save and close the PDF document 
finalDoc.Save("Sample.pdf");
finalDoc.Close(true);
 
Process.Start("Sample.pdf");
 

 

VB.NET

'Get files from folder
Dim filePaths As String() = Directory.GetFiles("../../Data/")
'Create the array list 
Dim mergearray As ArrayList = New ArrayList(filePaths.Length)
'Copy file path to the array list
For i = 0 To filePaths.Count() - 1
     mergearray.Add(filePaths(i))
Next
'Create a new PDF document 
Dim finalDoc As PdfDocument = New PdfDocument()
'Merge the PDF document 
For i As Integer = 0 To mergearray.Count - 1
    'Load an existing PDF 
    Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(mergearray(i).ToString())
    'Merge the PDF documnet 
    PdfDocument.Merge(finalDoc, loadedDocument)
Next
'Save and Close the PDF document 
finalDoc.Save("Sample.pdf")
finalDoc.Close(True)
Process.Start("Sample.pdf")

 

By executing the program, you will get the PDF document as follows.

Output document

A complete working sample can be downloaded from PdfSample.zip.

Take a moment to peruse the documentation to merge the multiple PDF documents with the string array and importing pages from multiple documents with Essential PDF. Also,  brief details about splitdelete, and rotate PDF pages are available with code examples.

Refer to here to explore a rich set of Syncfusion Essential PDF features.

An online sample link to merge PDF documents.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or 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 trail message.

 

See Also:

https://help.syncfusion.com/file-formats/pdf/merge-documents

https://www.syncfusion.com/kb/9146/how-to-merge-pdf-files-in-c-vb-net

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied