I am trying to insert a page break at a specific line of text.
This is what I have:
function replaceTextwithPageBreak(body, text){
var searchText = body.findText(text);
var textEl = searchText.getElement();
var textCh = textEl.getParent().getChildIndex(textEl);
body.insertPageBreak(textCh);
}
var breakText = "break";
replaceTextwithPageBreak(body, breakText);
This works, however.. in the Test Document Template I have set up, the text is as follows:
value1
value2
break (<-- the value I'm trying to insert the page break at)
value4
What is happening is everything is getting put onto the new page instead of just everything after 'break'.
Thanks in advance.
In your showing script of replaceTextwithPageBreak
, how about the following modification?
function replaceTextwithPageBreak(body, text) {
var searchText = body.findText(text);
var textEl = searchText.getElement().getParent();
var textCh = body.getChildIndex(textEl);
body.insertPageBreak(textCh);
}
By this modification, in your showing sample template, the page break is inserted to break
.
If there are multiple break
in the Document and you want to insert the line break to them, how about the following sample script?
function replaceTextwithPageBreaks(body, text) {
let s = body.findText(text);
while (s) {
const e = s.getElement();
const idx = body.getChildIndex(e.getParent());
body.insertPageBreak(idx);
// e.removeFromParent(); // If you want to remove "break" paragraph, please use this.
s = body.findText(text, s);
}
}