PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Jun 29, 2024 3:20 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue May 21, 2024 10:35 pm 
Offline

Joined: Tue May 21, 2024 10:29 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
PostPosted: Tue May 21, 2024 10:58 pm 
Offline

Joined: Tue May 21, 2024 10:29 pm
Posts: 2
Looks like I managed to solve it by adding the page.Width and page.Height calculations. If anyone sees any other improvements I could make, it would be much appreciated.

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
   page.Width = PdfSharp.Drawing.XUnit.FromInch(MyImage.Width / MyImage.HorizontalResolution)
   page.Height = PdfSharp.Drawing.XUnit.FromInch(MyImage.Height / MyImage.VerticalResolution)
   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


Top
 Profile  
Reply with quote  
PostPosted: Wed May 22, 2024 7:12 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 949
Location: CCAA
With PDFsharp, you can set the size of the pages, as you do.

You can also add two parameters when calling DrawImage to draw the image in the size you want.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 29 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group