Hi,
I was using 14.7 version of aspose, due to latest .word.lic file doesnot support older version so i updated it to 23.1 version. now my existing code for split pattern is not working.
public void IsolateTokenInForm(com.aspose.words.Document asposeDoc,
double formPackageVersion) throws Exception {
if (formPackageVersion < 1) {
try {
asposeDoc.getRange().replace(DEFAULT_TOKEN_PATTERN, "", new FindReplaceOptions(new SplitPatternToIsolatedNode()));
} catch (Exception e) {
logger.warn("Error in splitting pattern to isolate");
throw e;
}
}
}
private class SplitPatternToIsolatedNode implements IReplacingCallback {
// / This method is called by the Aspose.Words find and replace engine
// for each match.
// / This method highlights the match string, even if it spans multiple
// runs.
@Override
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.
// JAM: gets the zero-based starting position of the match from the
// start of the node that contains the beginning of the match.
if (e.getMatchOffset() > 0) {
currentNode = SplitRun((Run) currentNode, e.getMatchOffset());
}
// This array is used to store all nodes of the match for further
// highlighting.
// This can happen if some formmating or something else causes
// search result to split in to 2 or mode nodes.
ArrayList<Run> runs = new ArrayList<Run>();
// Find all runs that contain parts of the match string.
// JAM: getMatch() returns a java.util.regex.Matcher
// resulting from a single regular expression match during a Replace
int remainingLength = e.getMatch().group().length();
while (remainingLength > 0 && currentNode != null
&& currentNode.getText().length() <= remainingLength) {
runs.add((Run) 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((Run) currentNode);
}
if (runs.size() > 1) {
MergeRuns(runs);
}
return ReplaceAction.SKIP;
}
}
// Split any run at the given position
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, position));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}
// Merge the arraylist of runs
private void MergeRuns(ArrayList<Run> runs) throws Exception {
for (int i = 1; i < runs.size(); i++) {
runs.get(0).setText(runs.get(0).getText() + runs.get(i).getText());
runs.get(i).setText("");
}
}