Count Wrapped Lines in an Acrobat PDF Text Field with JavaScript
Question details
Count both explicit line breaks and visually wrapped lines in a PDF form text field using Acrobat JavaScript.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A multiline PDF form field contains text that may wrap because of the field's width.
- Observed behavior
- The desired count includes every displayed line, including lines created by automatic text wrapping.
Open the PDF in Acrobat and confirm the text field's exact name in Prepare a form. In the field's Properties, check that Multi-line is enabled, then enter enough text to wrap.
Test getMetrics for the Acrobat text field
Try the suggested field metrics method to see whether it returns displayed lines in your Acrobat version.
Availability and results may vary by Acrobat version. Test with text that contains both manual line breaks and automatic wrapping, and compare the result with the visible field.
In Acrobat, open the JavaScript debugger console. On Windows, you can use Ctrl+J; the menu location can vary by version.
Enter and execute this code, replacing yourTextField with the exact field name: var f = this.getField("yourTextField"); if (f && typeof f.getMetrics === "function") { var m = f.getMetrics(); console.println(m && m.lines ? m.lines.length : "Line metrics unavailable"); } else { console.println("Field or getMetrics unavailable"); }
Compare the number printed in the console with the lines visible in the field. Repeat after changing the text or field width to verify that wrapping is included.

Calculate wrapped lines using the field's layout
Use a custom calculation when the metrics method is unavailable or does not match the displayed text.
Work with PDF and Office files in WPS Office
WPS Office is a free, lightweight, easy-to-use Office alternative with a familiar interface and Microsoft Office format compatibility for a seamless move between suites. Acrobat-specific form JavaScript should still be tested in Acrobat.

Frequently Asked Questions
Does counting newline characters include wrapped lines?
No. Splitting the field value on line breaks counts explicit breaks only. Automatically wrapped lines depend on the field's layout and do not necessarily add newline characters to the value.
How can I find the text field's name?
Open Prepare a form in Acrobat, select the field, and check its name in the field list or Properties. Use that exact name in this.getField("fieldName").
What should I do if getMetrics is not available?
Use a custom calculation based on the field's usable width, font, font size, text, and wrapping rules. Test it against the rendered field because the result may be approximate.
Why does the line count change when I resize the field?
Changing the field width changes where text wraps. If you use a custom calculation, update its usable-width input and compare the result with the displayed field.




