cancel
Showing results for 
Search instead for 
Did you mean: 

Error - Merge PDF Files using PDFSharp DLL

holuomaVB
Level 3

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

 

9 REPLIES 9

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

Hello Dave,

Thank you for your response.

I’ve tried implementing the code as you suggested, including the exact external references and namespace, but I am encountering an error. The error message is as follows:

Description: Compiler error at line -22 in the upper section: Metadata file 'System' could not be found.

I’ve attempted to debug the issue, but I haven't been able to identify the cause of the error. Would you be able to provide further assistance or guidance on resolving this issue?

@holuomaVB Everything in the External References section has to end with ".dll", and it looks like I just left off typing that for System.


Dave Morris, 3Ci at Southern Company

Hi Dave, 

Yes I did add System.dll and yet the error persist. However, If I remove System and just add System.dll, it displays more errors (screenshot below)

holuomaVB_0-1736176867644.png

holuomaVB_1-1736177060212.png

 

 

 

The more errors situation is correct. The compiler doesn't even try to determine what actual errors you have if there is a prompt with one of the references. Leave it as "System.dll" and then we can work through each of those errors.

What is the full error message for the one that is cut off? It says like "PE image doesn't contain manage..."


Dave Morris, 3Ci at Southern Company

Alright. 

Page: Initialise
Stage: Stage1
Type: Error
Action: Validate
Description: Compiler error at top section line -19: Metadata file 'C:\Program Files\Blue Prism Limited\Blue Prism Automate\PdfSharp.dll' could not be opened -- PE image doesn't contain managed metadata.
Repairable: No

 

I didn't know what that error message meant, so I Googled it a bit. If I had to guess, it kind of sounds like the PdfSharp.dll file you have may be invalid in some way. Where do you get it from? And do you know how to try to get it again? There are a few ways to do this, but I tend to prefer going into Visual Studio, creating a temp project, installing a NuGet package, build it, and then get the DLL out of the bin folder of the project.

The PdfSharp.dll file I have I believe is an older version of PdfSharp from 2019. The file size is 523 KB.

Regardless, I feel like the PdfSharp file itself is your issue, and that is likely why you had issues before with your VB.NET code.

I would think that the latest version should work no matter what your version of Blue Prism is, but what Blue Prism version are you on?


Dave Morris, 3Ci at Southern Company

Hello Dave,

Thank you for your response. I’ve also been considering that the issue might be related to the PDFSharp.dll file. My current Blue Prism version is 7.2.1, and I’m not entirely sure if I can generate a DLL file myself. However, I’ll look into finding an alternative DLL file to see if that resolves the issue.

If you have any recommendations or additional insights, I’d greatly appreciate your guidance.