How to encapsulate any existing line with new bookmark in java

hi there,

1. we want to encapsulate any existing line with new bookmark using java. There is one line in doc with text “Hello How Can I Help You” now we want to cover that line with bookmark named “Initial Help”.

2. How can we add page break after “BREAK” key word in content (with multiple paragraphs) of any bookmark in document.

for example if below text text is in the bookmark called “Break_BK” and we want to apply page break after “BREAK” keyword in text.

This is a very pleasant gentleman who presents today to discuss his atrial fibrillation. When the patient goes into atrial fibrillation, he is completely incapacitated with, consider he has normal energy level at 10, his energy level can go down to 4 to 6. He becomes dizzy, lightheaded, weak and dyspneic with any form of physical activity with class III limitations, exacerbated stress and relieved by rest. When that occurs, there is no chest pain, no chest pressure, no palpitations or diaphoresis, no nausea, syncope or near-syncope. BREAK When he is in normal sinus rhythm, he is generally quite active.

He still helps out in his tree farm with his son with harvesting, pruning his trees. When he does so, he is typically limited just by dyspnea with class II symptoms, exacerbated by stress and relieved by rest. When that occurs, there is no no chest pain, no chest pressure, no palpitations, no diaphoresis, no syncope or near-syncope. He otherwise denies PND, orthopnea or lower extremity edema. He denies any CVA or TIA symptoms. He is on therapy with aspirin without melena, hematochezia, hemoptysis, or hematuria.

Please try using the following code:

Document doc = new Document("D:\\Temp\\in.docx");

FindReplaceOptions opts = new FindReplaceOptions();
opts.setDirection(FindReplaceDirection.BACKWARD);
opts.setReplacingCallback(new ReplacingCallback());

doc.getRange().replace(Pattern.compile("Hello How Can I Help You"), "" , opts);

doc.save("D:\\temp\\18.8.docx");

static class ReplacingCallback implements IReplacingCallback {

public int replacing(ReplacingArgs e) throws Exception {
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.getMatchNode();

// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.getMatchOffset() > 0)
currentNode = splitRun((Run) currentNode, e.getMatchOffset());

ArrayList runs = new ArrayList();

// Find all runs that contain parts of the match string.
int remainingLength = e.getMatch().group().length();
while ((remainingLength > 0) && (currentNode != null) && (currentNode.getText().length() <= remainingLength)) {
runs.add(currentNode);
remainingLength = remainingLength - currentNode.getText().length();

// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do {
currentNode = currentNode.getNextSibling();
} while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
}

// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0)) {
splitRun((Run) currentNode, remainingLength);
runs.add(currentNode);
}

DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
builder.moveTo((Run) runs.get(0));

builder.startBookmark("Initial Help");
BookmarkEnd end = builder.endBookmark("Initial Help");

Run lastRun = (Run) runs.get(runs.size() - 1);
lastRun.getParentNode().insertAfter(end, lastRun);

// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.SKIP;
}

/**
* Splits text of the specified run into two runs. Inserts the new run just
* after the specified run.
*/
private Run splitRun(Run run, int position) throws Exception {
Run afterRun = (Run) run.deepClone(true);
afterRun.setText(run.getText().substring(position));
run.setText(run.getText().substring((0), (0) + (position)));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}
}


You can use the same Find and Replace functionality of Aspose.Words for Java. Just use the following lines to insert page break:

...
...
DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
builder.moveTo((Run) runs.get(0));

builder.insertBreak(BreakType.PAGE_BREAK);
...
...