Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Counting the number of lines in a text field using JavaScript

New Here ,
Feb 11, 2022 Feb 11, 2022

Hi

Using JavaScript how can I count the number of lines in a text field including lines created as a result of wrapped text.

3.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2022 Feb 12, 2022

Hi.

 

Have you tried the getMetrics method?

 

For example:

 

console.log(this.yourTextField.getMetrics().lines.length);

 

 

Please let us know.

 

Regards,

JC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 12, 2022 Feb 12, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2022 Feb 12, 2022

Of course.

 

Here it is:

var _this = this;

_this.yourButton.on("click", function()
{
	console.log(_this.yourTextField.getMetrics().lines.length);
});
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 12, 2022 Feb 12, 2022

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 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2022 Feb 12, 2022

This is the Adobe Animate forums. I'm going to move this thread to the Adobe Acrobat forums.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2022 Feb 12, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 12, 2022 Feb 12, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2022 Feb 12, 2022

No, you will need to write it yourself, or hire someone to do it for you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2022 Feb 12, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2022 Feb 12, 2022

Maybe we should take a step back. Why do you need this information?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 12, 2022 Feb 12, 2022

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2022 Feb 12, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 13, 2022 Feb 13, 2022

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2022 Feb 13, 2022

Not in Acrobat, no.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2022 Feb 13, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 18, 2022 Mar 18, 2022
LATEST

Thank you so much @try67 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines