cancel
Showing results for 
Search instead for 
Did you mean: 

Error - Merge PDF Files using PDFSharp DLL

holuomaVB
Level 2

I am trying to creat an Object that merges PDF. I have installed PDFSharp DLL and added namespaces, but it seems to be giving errors. Is there anything am missing? Can anyone provide me a code to merge PDF uisng PDFSharp? Below is my code

Public Sub MergePDFs()
Try
' Input file paths
Dim inputPaths As String() = {"C:\path\file1.pdf", "C:\path\file2.pdf"}
Dim outputPath As String = "C:\path\merged.pdf"

' Create a new PDF document for output
Dim outputDocument As New PdfDocument()

' Loop through all input files
For Each pdfPath As String In inputPaths
If Not System.IO.File.Exists(pdfPath) Then
Throw New Exception($"File not found: {pdfPath}")
End If

' Open the input document
Dim inputDocument As PdfDocument = PdfReader.Open(pdfPath, PdfDocumentOpenMode.Import)

' Copy pages from input to output
For Each page As PdfPage In inputDocument.Pages
outputDocument.AddPage(page)
Next
Next

' Save the merged document
outputDocument.Save(outputPath)

Catch ex As Exception
Throw New Exception($"Error during PDF merge: {ex.Message}")
End Try
End Sub

 

2 REPLIES 2

david.l.morris
Level 15

What error(s) do you get?


Dave Morris, 3Ci at Southern Company

david.l.morris
Level 15

You have VB.NET so this may not help you, but here is some code that I used in the past for PDFSharp to merge Multiple files if you want to switch to C# or use this to compare to yours or rewrite in VB.NET:

Inputs:
[inputPaths] (DataTable) expecting either just one column or one of the columns to be named "Path".

[newFileName] (Text)
[outputFolderPath] (Text)

Outputs:
[outputFilePath] (Text)

 

External References:

System

System.Data.dll

System.Drawing.dll

PdfSharp.dll

System.Linq.dll

System.Collections.dll

System.Core.dll

 

Namespace Imports:
System

System.Drawing

System.Data

System.Diagnostics

System.IO

PdfSharp.Pdf

PdfSharp.Pdf.IO

System.Linq

System.Collections

System.Collections.Generic

System.Threading.Tasks

System.Text.RegularExpressions

System.Text

PdfSharp.Pdf.Content

PdfSharp.Pdf.Content.Objects

PdfSharp.Pdf.Advanced

 

// set up some variables
string[] files;

if (inputPaths.Columns.Count > 1)
{
	// get a string array out of the Path column
	files = inputPaths.Rows.OfType<DataRow>().Select(k => k["Path"].ToString()).ToArray();
}
else
{
	// get a string array out of a DataTable regardless of the Field Name
	files = inputPaths.Rows.OfType<DataRow>().Select(k => k[0].ToString()).ToArray();
}

if (string.IsNullOrEmpty(newFileName))
{
	newFileName = Path.GetFileNameWithoutExtension(files[0]) + "_merged";
}
else
{
	newFileName = Path.GetFileNameWithoutExtension(newFileName);
}

if (string.IsNullOrEmpty(outputFolderPath))
{
	outputFolderPath = Directory.GetParent(files[0]).ToString();
}

outputPath = Path.Combine(outputFolderPath, newFileName + ".pdf");

// Open the output document
PdfDocument outputDocument = new PdfDocument();

// Iterate files
foreach (string file in files)
{
	// Open the document to import pages from it.
	PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);

	// Iterate pages
	int count = inputDocument.PageCount;
	for (int idx = 0; idx < count; idx++)
	{
		// Get the page from the external document...
		PdfPage page = inputDocument.Pages[idx];
		// ...and add it to the output document.
		outputDocument.AddPage(page);
	}
}

// Save the document...
outputDocument.Save(outputPath);

 

 


Dave Morris, 3Ci at Southern Company