Position Difference when Scaling is Different. What should I have done?

Hi all, I am trying to add text at position near a Signature section but due on scaling, the position can get very off.

I calculate scale based on the following:

user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
dpi = user32.GetDpiForSystem()
scale =  dpi / 96

I could have understand if as the scale gets larger, 1.25 → 1.5, the height points required is higher, but for some reason, it ended up as follows:
100%
image

125%
image

150%
image

The above is when the date_width = 119 and date_height = 760 are constant.

Currently, this is what I am using to hardcode based on the scaling.

    date_width = 119
    date_height = 760
    if scale == 1.25:
        date_width = 119
        date_height = 735
    elif scale == 1.5:
        date_width = 119
        date_height = 770

page.insert_text((date_width, date_height), date_str, fontsize=10, fontname="helv", fill=(0, 0, 0))

But honestly, it’s not the most dynamic way to do it.
So I was wondering if there’s better way to do this.

Hi @yxpoh !

I think it might be easier to locate the “Signature & Date” text position and then use insert_text to position the date near to that point. Note, insert_text uses an x,y point position whereas you are using width & height.

So I would try using Page.search_for and look for “Signature & Date” then work out from the rectangle results you get there how to position the date x,y position. ( Basically it has to be close to that right? ).

I see. I can give that a try.

Oh I was using width and height as the name initially because that was how I tried to use it first, but after the changes, I kept the naming convention.

Got it! Hopefully if you use an offset position from the location you can find a X & Y which should always work for your use case. Good luck!

@JaeJaeJaeJae
Thank you!

It worked! Instead of the full word, I specifically choose to search for “Employee Signatu” so that the search will have the rectangle stop at the “r” and allow me to just adjust the y coordinates upwards.
(Ended up letting the date start off after the “Signature” word since there’s still space.)

125% (Rect(39.119998931884766, 741.85498046875, 112.41598510742188, 751.8900146484375))
image

150% (Rect(39.119998931884766, 776.0549926757812, 112.41598510742188, 786.0900268554688))
image

Just for clarity, I have now converted to the following codes as suggested by Jamie.

# Search for coordinates for the string and use the string to offset for where to place the date.
    sign_loc = page.search_for("Employee Signature")[0]
    print(f'Signature Location: {sign_loc}')
    # === Date formatting ===
    date_str = datetime.today().strftime("%d/%m/%Y")

    # === Insert Date based on search string above===
    page.insert_text((sign_loc.x1, sign_loc.y0 - 4), date_str, fontsize=10, fontname="helv", fill=(0, 0, 0))

I must say some of the config is hard coded because the file I am using only have that single section of text “Employee Signature” otherwise, there will be checking that needs to be done.

the print is more for a debug and output during execution so I can see what’s wrong.

1 Like

@yxpoh That’s great - really glad that the solution worked for you! :slight_smile: