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
  • 4170 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

    SamKhaMAuthor
    Known Participant
    March 18, 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.


    Thank you so much @try67 

    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

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

    Of course.

     

    Here it is:

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