PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 4:06 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: Fri Sep 08, 2023 5:52 pm 
Offline

Joined: Fri Sep 08, 2023 5:27 pm
Posts: 12
Hi - I've been tasked to see if PdfSharp can replace another 3rd party tool my company uses for filling out Pdfs. These PDFs are template files with PdfTextFields that data from a db will fill in. Things are working great except for this situation where I've got a PdfTextField that I'm placing an image (a signature) in.

The image renders but not in the place I expect, so the X & Y of the XRect for the PdfTextField are off for some reason (note - this field renders fine using another company's Pdf tool). I'm wondering if there is a "printable area" verses a page's Width and Height that I need to take into account. It's the "Y" value in particular that seems off: it prints the signature near the top of the page, not the bottom. Because of this, I flipped the Y value upside down by adding the code "page.Height - ..." to the Y calculation, thinking perhaps PdfSharp thinks Y starts at the top instead of the bottom of the page (I'm new to this tool...). Doing that printed the signature much closer to where it should be, but was about an inch too low, making it run off the bottom of the page. This made me wonder about the printing area verses a page's natural height & width. Note that the code "AppStd.asFloat()" below simply converts an object into a float/double.

Code:
                        PdfArray rectElem = (PdfArray)imageField.Elements["/Rect"];
                        if (rectElem != null) {
                            PdfPage page = doc.Pages[0]; // default to page 0
                            double x = AppStd.asFloat(rectElem.Elements[0].ToString());
                            double y = [b]page.Height - [/b]AppStd.asFloat(rectElem.Elements[1].ToString());
                            double w = AppStd.asFloat(rectElem.Elements[2].ToString());
                            double h = AppStd.asFloat(rectElem.Elements[3].ToString());
                            XRect rect = new XRect(x, y, w, h);
                            PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
                            gfx.DrawImage(image, rect);

Is there another parameter of the Page that I'm not taking into account as far as placement of elements on the page? Any help would be appreciated.
Thank you,
Chris B


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 11, 2023 9:39 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
I think this is a case where you have to call "gfx.Transformer.WorldToDefaultPage(rect)" to transform the co-ordinates.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 11, 2023 1:59 pm 
Offline

Joined: Fri Sep 08, 2023 5:27 pm
Posts: 12
That totally worked! Thank you!
Chris B :mrgreen: :mrgreen:


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: No registered users and 424 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