How to convert tiff files into single PDF file using uipath activities?

Need convert multiple tiff files into single pd

@Nagarjunas

Welcome to the uipath community.

We do not have any activities to convert Tiff file into PDF file.

Open one by one Tiff file and then save it as PDF files.

You can use PDF Sharp

Dim destinaton As String = in_PDF_Destination
Dim MyImage As System.Drawing.Image = system.Drawing.Image.FromFile(in_TIF_Location)
Dim doc As PdfDocument = New PdfDocument()

For PageIndex As Integer = 0 To MyImage.GetFrameCount(FrameDimension.Page) - 1
    MyImage.SelectActiveFrame(FrameDimension.Page, PageIndex)
    Dim img As pdfsharp.Drawing.XImage = pdfsharp.Drawing.XImage.FromGdiPlusImage(MyImage)
    Dim page As PdfPage = New PdfPage()

    If img.Width > img.Height Then
        page.Orientation = PageOrientation.Landscape
    Else
        page.Orientation = PageOrientation.Portrait
    End If

    doc.Pages.Add(page)
    Dim xgr As XGraphics = XGraphics.FromPdfPage(doc.Pages(PageIndex))
    xgr.DrawImage(img, 0, 0)
Next

doc.Save(destinaton)
doc.Close()
MyImage.Dispose()
2 Likes

Good one @bradsterling
@Nagarjunas - also you can do it by importing pdftextsharp dll

Thank you

I have the pdfsharp package installed by I’m getting BC30002: Type ‘PdfDocument’ is not defined. At line 3 error, I’m not sure what am I missing if any of you could explain that would be really helpful.

You have to type the entire reference: PdfSharp.Pdf.PdfDocument

There are multiple places you’ll have to do this.

I’ve gotten as far as this code:

Try
Dim MyImage As System.Drawing.Image = System.Drawing.Image.FromFile(sourcefile)
Dim doc As PDFSharp.Pdf.PdfDocument = New PDFSharp.Pdf.PdfDocument()

For PageIndex As Integer = 0 To MyImage.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page) - 1
	MyImage.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, PageIndex)
	Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile(sourcefile)
	Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage
	page.Orientation = PdfSharp.PageOrientation.Landscape
	doc.Pages.Add(page)
	Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(doc.Pages(PageIndex))
	xgr.DrawImage(img, 0, 0)
Next

doc.Save(destinationfile)
doc.Close()
MyImage.Dispose()
Catch ex As Exception
	Console.WriteLine(ex)
End Try

The result is…

image

I had tried it with PdfSharpCore instead of PdfSharp and it worked the same, and gave the same exception but was a little more descriptive. TIF was not listed as one of the supported image formats.

Note that I changed the “Dim img” line to Ximage.FromFile instead of XImage.FromGdiPlusImage because there is no FromGdiPlusImage as a method of PdfSharp.Drawing.Ximage

I got it fully working and wanted to share my code for anyone who needs it.

Install the PDFSharp-GDI package (I am using v6.0.0).

Invoke Code with arguments sourcefile and destination file, using this code:

Try
Dim MyImage As System.Drawing.Image = System.Drawing.Image.FromFile(sourcefile)
Dim doc As PDFSharp.Pdf.PdfDocument = New PDFSharp.Pdf.PdfDocument()

For PageIndex As Integer = 0 To MyImage.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page) - 1
	MyImage.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, PageIndex)
	Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromGdiPlusImage(MyImage)
	Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage
	page.Orientation = PdfSharp.PageOrientation.Portrait
	doc.Pages.Add(page)
	Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(doc.Pages(PageIndex))
	xgr.DrawImage(img, 0, 0)
Next

doc.Save(destinationfile)
doc.Close()
MyImage.Dispose()
Catch ex As Exception
	Console.WriteLine(ex)
End Try

I get tons of errors. Can you share a xaml?

What errors did you get? There is no XAML. This is generic code that can be used in an Invoke Code to convert an image to a PDF.

I had to change the packages. Added soms code myself to make it fit on a4.

You use it in a xaml right? I thought that could be shared.