How to know the maximum number of pixels that fits on pdf field?

Hi,

I’m starting an automation where the main goal is to fill some fields of a pdf. The task is mostly simple, but i’m having some difficulties in one specific field of the pdf.
The address field of the PDF is limited in terms of pixels, and some strings doesn’t fit there. In that cases, I’ve to throw a business exception.
The question here is: there is an easy way to know the exact limit of that field? And convert the string previously to pixels to know how many pixels it will correspond?

Hi,

are you sure the field is limited by pixels and not by number of characters?
You could find these out by trial and error or by examining the pdf file with pdf software. Form field properties could look something like this:

image

Counting characters in strings is pretty easy, just use String.Length

Best,
Lukas

Thank you for your answer.
Count the number of characters is very easy and that’s not the problem here. The field isn’t constant in terms of characters. For example: It’s possible to write 36 “i” characters but only fits 30 “a” characters. So I found that the field is limited by the number of pixels and not by the number of characters.

Wow, funky requirement. I tested around a bit for you and I think I got it working by using a TextRenderer.

  1. Take your string
  2. Set up your font (System.Drawing.Font) - Example: SampleFont = New Font("Arial",12)
  3. Import the namespace System.Windows.Forms
  4. Calculate the Width in pixels (Int32) - Example: System.Windows.Forms.TextRenderer.MeasureText(SampleString,SampleFont).Width
  5. Compare text width with your form limit

That’s it! Simple :slight_smile:

References: TextRenderer.MeasureText Method (System.Windows.Forms) | Microsoft Docs
Font Class (System.Drawing) | Microsoft Docs

1 Like

Thank you very much for your answer! You taught me something that I’ve never heard or read about.

I still have a doubt: I don’t know how to get the limit of the field. There is an easy way to get the limit value of pixels of that specific field?

You’re very welcome!

Don’t know how to get the limit of the field. My first guess is still the best - either trial and error or trying to inspect the form with the right tool…

Let me know how it goes

The field is not limited by the number of pixels. I can insert 470 pixels of “A” and in the same field fits 308 pixels of “I”. Therefore, i can’t fulfill this requirement because the limit of pixels of the field isn’t constant.

Once again, thank you for your help!

Cheers

If it’s not limited by characters and not limited by pixels - what is it limited by then?!

Did you make sure to set the right font and font size for the text renderer?

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.