Friday
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
Friday
What error(s) do you get?
Friday - last edited Friday
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);
yesterday
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?
yesterday
@holuomaVB Everything in the External References section has to end with ".dll", and it looks like I just left off typing that for System.
yesterday - last edited yesterday
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)
yesterday
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..."
yesterday
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
yesterday
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?