PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Placing an image into PdfTextField rectangle - placement on
http://forum.pdfsharp.de/viewtopic.php?f=2&t=4471
Page 1 of 1

Author:  chris.ball [ Fri Sep 08, 2023 5:52 pm ]
Post subject:  Placing an image into PdfTextField rectangle - placement on

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

Author:  Thomas Hoevel [ Mon Sep 11, 2023 9:39 am ]
Post subject:  Re: Placing an image into PdfTextField rectangle - placement

I think this is a case where you have to call "gfx.Transformer.WorldToDefaultPage(rect)" to transform the co-ordinates.

Author:  chris.ball [ Mon Sep 11, 2023 1:59 pm ]
Post subject:  Re: Placing an image into PdfTextField rectangle - placement

That totally worked! Thank you!
Chris B :mrgreen: :mrgreen:

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/