Thanks for your inquiry. Could you please share your input documents along expected output document here for our reference? We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.
The text Calculation Agent EUR is inside two table’s cell. In your case, we suggest you please call the Range.Repalce method twice as shown below. Please get the code of ReplaceEvaluatorFindAndHighlight in above article. Hope this helps you.
We have logged a feature request as WORDSNET-15646 in our issue tracking system to search the text in a table’s row. You will be notified via this forum thread once this feature is available. We apologize for your inconvenience.
Thanks for your feedback. We will appreciate it if you please share your sample input and expected output document here. It will help us to understand your requirements exactly and address it accordingly.
Thanks for your patience. [quote=“madhusanmani, post:5, topic:160222”]
i tried with the belwo code, but it highlighted all teh instance of the Word document not only the
Calculation Agent : EUR.
My requirement is to only highlight the above line instance of Calculation Agent and EUR"
[/quote]
Please use the following code example to achieve your requirement. Hope this helps you.
Document doc = new Document(MyDir + "in.docx");
Pattern regex = Pattern.compile("EUR", Pattern.CASE_INSENSITIVE);
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.setReplacingCallback(new ReplaceEvaluatorFindAndHighlight("Calculation Agent"));
doc.getRange().replace(regex, "", findReplaceOptions);
doc.save(MyDir + "17.7.docx");
----------------------------------------------------------------
class ReplaceEvaluatorFindAndHighlight implements IReplacingCallback {
String text;
ReplaceEvaluatorFindAndHighlight(String text)
{
this.text = text;
}
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());
// This array is used to store all nodes of the match for further highlighting.
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);
}
Cell cell = (Cell)((Run)runs.get(0)).getAncestor(NodeType.CELL);
if(cell != null)
{
if(cell.getPreviousSibling().toString(SaveFormat.TEXT).trim().equals(this.text))
{
// Now highlight all runs in the sequence.
for (Run run : (Iterable<Run>) runs)
run.getFont().setHighlightColor(Color.YELLOW);
//Uncomment the following lines if you want to highlight text "Calculation Agent"
//for (Run run : (Iterable<Run>) ((Cell)cell.getPreviousSibling()).getChildNodes(NodeType.RUN, true))
// run.getFont().setHighlightColor(Color.YELLOW);
}
}
// 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;
}
}
If you want to compare two documents and highlight the changes, please use the following code example. Hope this helps you.
Thanks for your inquiry. Please call Document.acceptAllRevisions method before saving the document. This method accepts all tracked changes in the document. Hope this helps you.
If you still face problem, please manually create your expected output Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.