I have installed the UiPathTeamPDF.Extensions.Activities, which includes PDFSharp as a dependency.
In VB.Net code (Visual Studio), I am able to easily convert my TIFF files into separate PDF files and combine them. I would like to use this same code in an Invoke Code block, but I am unable to access or import the PDFSharp libraries.
If there is no way to do this in a UiPath Invoke Code block, are there activities in the PDF.Extensions that can perform these same tasks? I do not see an activity to read a TIFF, nor do I see any activity to combine PDF documents. I see some activities around Word documents, but that isn’t what I’m trying to accomplish.
if you have that package installed and pdfshard is a dependency, you should be able to use just like you do in visual studio, inside an invoke code activity…
Are there other packages/imports that I have to make? UiPath complains a lot of the form:
error BC30002: Type ‘PdfSharp.Pdf.PdfDocument’ is not defined. At line 23
error BC30002: Type ‘PdfSharp.Pdf.PdfPage’ is not defined. At line 24
etc. I am fully qualifying the type names in the code, such as:
Dim doc As New PdfSharp.Pdf.PdfDocument 'Document to write to
Dim Page As New PdfSharp.Pdf.PdfPage 'A new PDF page to add to the document
i dont know what you did wrong, but as soon as i installed UiPathTeam.PDF.Extensions.Activities, added an invoke code activity, everything worked normally, no need to add any imports manually…
Yes, PDFSharp appears in the package manager. I thought perhaps there were Activities (e.g. “UI Automation”, “App Integration”, etc.) that were related.
Code is of the form:
Dim inputPath As String = "c:\temp\TIFFS"
Dim outputFilePath As String = "c:\temp\TIFFS\tiff_converted_to_PDF.pdf"
'Clear out the old documents
For Each deleteFile As String In Directory.GetFiles(inputPath, "*.*", SearchOption.TopDirectoryOnly)
File.Delete(deleteFile)
Next
'Write each TIFF file out
Dim counter As Integer = 0
For Each tmpStr As String In tiffList
counter = counter + 1
Dim bytes As Byte() = Convert.FromBase64String(tmpStr)
File.WriteAllBytes(inputPath + "\bytefileoutput_" + counter.ToString() + ".tiff", bytes)
Next
'Convert each TIFF into a PDF
For Each foundFile As String In Directory.GetFiles(inputPath, "*.tif*")
Dim destinaton As String = inputPath + "\" + Path.GetFileName(foundFile).Replace(".tiff", "") + ".pdf"
Dim doc As New PdfSharp.Pdf.PdfDocument 'Document to write to
Dim Page As New PdfSharp.Pdf.PdfPage 'A new PDF page to add to the document
Page.Orientation = PdfSharp.PageOrientation.Portrait
doc.Pages.Add(Page)
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile(foundFile) 'Reads a TIFF into an XImage
'Write the TIFF image to the new page that was added to the final PDF
Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(doc.Pages(0))
xgr.DrawImage(img, 0, 0)
xgr.Dispose()
doc.Save(destinaton) 'Save the final document
doc.Close() 'Close the final document
Next
'Now combne all of the created PDF documents into one
Dim outpdf As New PdfSharp.Pdf.PdfDocument
For Each foundFile As String In Directory.GetFiles(inputPath, "*.pdf*")
Dim inputDocument As PdfSharp.Pdf.PdfDocument = PdfSharp.Pdf.IO.PdfReader.Open(foundFile, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import)
For iCounter = 0 To inputDocument.PageCount - 1
outpdf.AddPage(inputDocument.Pages(iCounter))
Next
Next
outpdf.Save(outputFilePath)
'Now get the Base64 representation of the created PDF file
Dim byteArray As Byte() = System.IO.File.ReadAllBytes(outputFilePath)
finalStr = Convert.ToBase64String(byteArray)
as a tip, dont do loops like this: For Each deleteFile As String In Directory.GetFiles(inputPath, "*.*", SearchOption.TopDirectoryOnly)
create a variable for the getfiles instead or it will be evaluated at every iteration…
We have determined that the PDFSharp package doesn’t work in the 2017 version of UiPath. We are in the process of updating our systems to the 2019 version and as a result, this particular bot will be upgraded sooner than originally planned.