PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
XForm, DPI, Page Size, etc https://forum.pdfsharp.net/viewtopic.php?f=2&t=2081 |
Page 1 of 1 |
Author: | BadSyntax [ Mon Jul 09, 2012 5:03 pm ] |
Post subject: | XForm, DPI, Page Size, etc |
First of all thanks for a great product, I didn't realize creating PDFs could be so easy, and mirroring .NET classes like drawing make conversion SOOOOO easy... But I'm having an issue, not a software bug, but a me not knowing what I'm doing bug. Here is what I'm doing: Create an XForm, 2400x3150 pixels Draw an image onto the XForm, same size as the XForm, at 0,0 Add a page, width/height same as the xform Draw the xform onto the page at 0,0 Draw text/images/etc onto the page Add page to a document Now, it all works, however the output is roughly 75% of the size it should be. I think this is a 72 DPI/96 DPI conversion issue. When I define the form I create it as xunit.frompoint, and I use pixels everywhere. I'm trying to just load a background image, then draw stuff at specific x/y coordinates on the pages. I can get it to work by dividing the page size and all my coordinates by 1.25, but that seems kinda silly and I think I'm just not getting something here. So I guess my question is what is the relationship between the size of an XForm, a page defined as the same size, and drawing things onto the page at specific locations. I want to get rid of all my "/ SizeMod" snippets of code and better understand how to create PDFs. I'm also not real sure how I should define all my margins, though I have been fudging it by just drawing the stuff at particular offsets. Setting a margin and then using 0,0 as my drawing offset instead of 50,50 or whatever would also be helpful. And a last thing, my pages are like 90% white space, but still nearly 1mb in size due to the size of the background graphic (its a frame basically), any way to decrease that? Thanks in advance. Here is my code (Sorry its in VB.NET, {} give me a headache, and VB.NET has yet to show me a limitation): Code: 'Create Document
Dim PDF_Doc As New PdfSharp.Pdf.PdfDocument Dim PDF_Font As New PdfSharp.Drawing.XFont("Arial", 16) 'Create a new bitmap from a 2400 x 3150 pixel file Dim BMP As New Bitmap("C:\Temp_Background.png") 'Create the background form Dim PDF_Form As New PdfSharp.Drawing.XForm(PDF_Doc, 2400, 3150) 'PdfSharp.Drawing.XUnit.FromPoint(2400), PdfSharp.Drawing.XUnit.FromPoint(3150)) 'Draw image onto background form Dim FormGfx As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromForm(PDF_Form) FormGfx.DrawImage(BMP, 0, 0) 'A divider I have to use in order for the pages to render correctly Dim SizeMod As Single = 1.25 'Create new page Dim Page As New PdfSharp.Pdf.PdfPage(PDF_Doc) Page.Size = PdfSharp.PageSize.A4 Page.Width = 2400 / SizeMod Page.Height = 3150 / SizeMod 'Draw background form onto page Dim Gfx As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(Page) Gfx.DrawImage(PDF_Form, 0, 0, 2400, 3150) 'Create outline/bookmark for this page Dim PDF_Outline As New PdfSharp.Pdf.PdfOutline("Page1", Page) PDF_Doc.Outlines.Add(PDF_Outline) 'Load up an image to draw onto the page Dim TempBMP As Bitmap = Bitmap.FromFile("C:\Temp_Image.png") TempBMP.MakeTransparent(TempBMP.GetPixel(0, 0)) 'Draw the image onto the page Gfx.DrawImage(TempBMP, 930 / SizeMod, 650 / SizeMod, 626 / SizeMod, 811 / SizeMod) 'Draw some text on page, I'm giving a 50 pixel border on top/left, though I'm not sure this is the best method Gfx.DrawString("Page 1", PDF_Font, PdfSharp.Drawing.XBrushes.Red, New PdfSharp.Drawing.XPoint((50 + 27) / SizeMod, (50 + 764) / SizeMod)) 'Add to document PDF_Doc.AddPage(Page) PDF_Doc.Close() PDF_Doc.Save("C:\PDFTest.pdf") |
Author: | Thomas Hoevel [ Tue Jul 10, 2012 8:14 am ] |
Post subject: | Re: XForm, DPI, Page Size, etc |
Hi! BadSyntax wrote: Now, it all works, however the output is roughly 75% of the size it should be. I think this is a 72 DPI/96 DPI conversion issue. Maybe yes, maybe no.PDF files have no pixels, they are vector based. PDF pages have no pixels - they have a size specified in inches, centimeters, or points (72 point are an inch). Generally it's a good idea to specify the destination rect when drawing an image - that will solve the problem. If no size is specified, the image will be drawn with the size specified in the image (maybe 96 dpi in your case). |
Author: | BadSyntax [ Sat Jul 14, 2012 7:42 am ] |
Post subject: | Re: XForm, DPI, Page Size, etc |
Well a LOT of "1 quick change, 30 seconds to wait for results.... repeat", but I think I got it. First, PDFSharp doesn't behave quite like the GDI, when you draw text, it draws up and right from the point you specify. The GDI draws down and right, so I ended up having to drop all my points down by the size of the font. Here is how I'm doing it now, if it helps anybody else: Step 1. I specify the original form size as the same size as the background PNG I load. Step 2. I create xgraphics from the form, and draw the original PNG at 0,0 unscaled Step 3. I create a page (this is for each page) Step 4. I set the size to A4, the width/height to the same as the form/PNG size, and then I set all the margins to about 1/34 or 1/44 of the size (for .25" basically) I can then draw things using the xgraphics object of the page at normal pixel coordinates. They aren't perfect, but they are close enough. The only "hack" I have to do is add the size of the font to the Y coordinate whenever I draw text (graphics are right, just not text). I was also disappointed to find that I couldn't draw a filled in ellipse with PDFSharp, which I was using as a darker background on some circles. Overall it works pretty well, and it does the job, but there are some behaviors that aren't quite right IMO. If you want your PDFs small, avoid graphics at all costs. And if you do want graphics in them, you MUST scale the image down to a smaller size before rendering it. Even though your render region may only be 10,10 pixels, that 1000,1000 image in a PDF is rendered in its maximum resolution, even though on the page it looks small. Scaling the images with the GDI before rendering reduced my file size by over 90%. |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |