Counting the number of lines in a text field using JavaScript
Copy link to clipboard
Copied
Hi
Using JavaScript how can I count the number of lines in a text field including lines created as a result of wrapped text.
Copy link to clipboard
Copied
Hi.
Have you tried the getMetrics method?
For example:
console.log(this.yourTextField.getMetrics().lines.length);
Please let us know.
Regards,
JC
Copy link to clipboard
Copied
I did not try this function before. Please could your proivide with a sample script using this fuction to call from a Button. I tried the following code and it did not work.
var txtField=this.getField("Labels");
app.alert(txtField.getMetrics().lines.length);
I received the following error:
TypeError: txtField.getMetrices is not function
Copy link to clipboard
Copied
Of course.
Here it is:
var _this = this;
_this.yourButton.on("click", function()
{
console.log(_this.yourTextField.getMetrics().lines.length);
});
Copy link to clipboard
Copied
Thank you
To test your function I created a Button on Adobe Acrobat Pro DC Form.
Under Button Properties, Actions, Run a Java Script.
I pasted the following code:
var _this = this;
console.log(_this.yourTextField.getMetrics().lines.length);
When I click on the Button I receive the following error.
TypeError: this.yourTextField is undefined
1:AcroForm:Button3:Annot1:MouseUp:Action1Exception in line 3 of function top_level, script AcroForm:Button3:Annot1:MouseUp:Action1
I have the following questions:
- Am I doing the right thing
- How do I get the field that I need to read its number of lines
- How can I get the number of lines in this text field
My scenario is that I want to create a button on a Adobe Acrobat Pro DC Form so whenever I click on that button I get a specific text field by name then I get the number of lines written in this text field.
Could you please provide me with a sample code that does the abode scenario
Copy link to clipboard
Copied
This is the Adobe Animate forums. I'm going to move this thread to the Adobe Acrobat forums.
Copy link to clipboard
Copied
There's no built-in way to do it. You will need to write a custom function to calculate the length of the text based on the font, font size and the characters used in it.
Copy link to clipboard
Copied
Could you please provide me with a sample code that calculates the length of the text based on the font, font size and the characters used in it. I am using Font "Calibri" size 11
Copy link to clipboard
Copied
No, you will need to write it yourself, or hire someone to do it for you.
Copy link to clipboard
Copied
Basically, you will need to create an array of all possible characters, with the width each one takes in this font, and then go over your text one character at a time, looking it up in this array, and adding up the width values, until you've reached the width of the field. This is quite a complex and labor-intensive job.
Copy link to clipboard
Copied
Maybe we should take a step back. Why do you need this information?
Copy link to clipboard
Copied
I need to press on a button to collect texts from some text fields then add them to a text field "Labels1" where it should not exceed a certain number of lines. I wrote the following script to check the number of lines in the "Labels1" field that works fine but does not get the number of lines that ocure as a result of along wrapped text. I need a sample scrip that helps me get the number of lines that ocure as a result of the along wrapped text to add it to the followng script.
var labField=this.getField("Labels1");
var myString=labField.value;
var splits=myString.split(/\r|\r\n|\n/);
app.alert(splits.length);
Copy link to clipboard
Copied
Well, as I wrote, it's a very complex task. You also need to take into account the length of the words, not just the characters, as line-wrapping occurs at the word-level.
Copy link to clipboard
Copied
Is there a function that I can use to measure the width and height of a string in inches or mm or cm or any other measurement factor?
Copy link to clipboard
Copied
Not in Acrobat, no.
Copy link to clipboard
Copied
Actually, you might be able to do it by using the getPageNthWordQuads method, which returns the location of a piece of static text on the page. You will need to write out each letter on its own, use this method to read its location and then parse the results to calculate the width and height of that letter.
If you're interested, I could develop this script for you, for a fee. You can contact me privately via PM (click my username and then on "Send a message") to discuss it further.
Copy link to clipboard
Copied
Thank you so much @try67

