PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
Converting TIF to PDF - pages are being cut off https://forum.pdfsharp.net/viewtopic.php?f=2&t=4595 |
Page 1 of 1 |
Author: | postwick [ Tue May 21, 2024 10:35 pm ] |
Post subject: | Converting TIF to PDF - pages are being cut off |
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 |
Author: | postwick [ Tue May 21, 2024 10:58 pm ] |
Post subject: | Re: Converting TIF to PDF - pages are being cut off |
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 |
Author: | TH-Soft [ Wed May 22, 2024 7:12 am ] |
Post subject: | Re: Converting TIF to PDF - pages are being cut off |
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. |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |