Skip to main content
Known Participant
February 11, 2022
Question

Counting the number of lines in a text field using JavaScript

  • February 11, 2022
  • 2 replies
  • 4169 views

Hi

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

    This topic has been closed for replies.

    2 replies

    try67
    Community Expert
    Community Expert
    February 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.

    SamKhaMAuthor
    Known Participant
    February 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

    try67
    Community Expert
    Community Expert
    February 12, 2022

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

    JoãoCésar17023019
    Community Expert
    Community Expert
    February 12, 2022

    Hi.

     

    Have you tried the getMetrics method?

     

    For example:

     

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

     

     

    Please let us know.

     

    Regards,

    JC

    SamKhaMAuthor
    Known Participant
    February 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

    SamKhaMAuthor
    Known Participant
    February 12, 2022

    Of course.

     

    Here it is:

    var _this = this;
    
    _this.yourButton.on("click", function()
    {
    	console.log(_this.yourTextField.getMetrics().lines.length);
    });

    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