Skip to main content
Participant
May 6, 2019
Answered

Splitting Field contents based on line count

  • May 6, 2019
  • 2 replies
  • 791 views

I need a solution in place to split a multi line field at a certain point and fill the remaining split text into another field. I have three multi-line fields named Field 1, Field 2, and Field 3. I should say that data is not typed into these fields but rather is populated via a series of functions that grabs data from the other form fields and summarizes results in Field 1. If data does not fit in Field 1 I would like the text to overflow into Field 2..and so on. I already have the code to populate the fields.

I had the idea that the easiest way to do this count the number of paragraph returns and then split when a certain number is reached. I know how to count the returns but not sure how to implement splitting overflow..

So for example, if I have this code:

var count = 0;

var s = getField("Field1").valueAsString;

for (var i = 0; i < s.length; i++) {
    if (s === "\r") count += 1;
}

....and I want to split the text at 50 returns.. then fill Field2.

Can this be done? Maybe there is an easier way than counting returns?

Thanks in advance.

This topic has been closed for replies.
Correct answer try67

You can use the slice method to return part of an array. See: JavaScript Array slice() Method

2 replies

Thom Parker
Community Expert
Community Expert
May 6, 2019

Here's an article on almost this exact topic.

https://acrobatusers.com/tutorials/splitting-and-rebuilding-strings

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
May 6, 2019

You can use the slice method to return part of an array. See: JavaScript Array slice() Method