I am a RPA developer and needed a way to convert TIF files to PDF for an automation. I know very little about PDFSharp but with some research I came up with the below code. It works but we noticed an issue today. For documents with legal size pages (8.5x14) it's cutting them off when generating the PDF. The resulting PDF has all pages letter size (8.5x11). What can I add to the below code so it detects the page size in the TIF and sets it correctly for each page in the PDF?
(sourceFile is an argument passed in with the path and filename of the TIF file)
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