How to transform TIFF into PDF and combine?

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.

1 Like

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…

Good afternoon TimD,

The answers in this thread may help you out:

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

The package manager shows PDFsharp installed; the namespaces don’t show up as options in the Imports tab.

Is this value available in the Import namespace area? pdftextsharp.dll

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…

It is not.

I went to a different VDI and installed the Activities. Nothing is showing up relating to PdfSharp…

what do you mean as show up? it just should not give you error for not finding the namespaces…

also if pdfsharp is the only thing you need from that package, you can just install it
image

Yes, PDFSharp appears in the package manager. I thought perhaps there were Activities (e.g. “UI Automation”, “App Integration”, etc.) that were related.
image

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)

When running, error shows:
image

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…

Code has been updated on your recommendation. I’m having somebody else try to see if they can run this since I’ve not been able to.

you might have some problems if you install several versions of the same assembly tho…

There appears to only be one installed at this point.

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.

1 Like