This is my FindAndReplaceOptions
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.setDirection(FindReplaceDirection.FORWARD);
findReplaceOptions.setMatchCase(true);
findReplaceOptions.setFindWholeWordsOnly(true);
findReplaceOptions.setReplacingCallback(new ReplaceXTimes(1));
The callback is getting triggered even for the matches that doesn’t satisfy the above conditions like FindWholeWordsOnly. Is that expected?
How do I know in CallBack that for which call, replacement happened.
For one example, I saw it getting called more than 10 times, but replacement happened only twice.
@gkumar16 Could you please attach your document and specify what search pattern is used? We will check the issue with FindWholeWordsOnly
and provide you more information. Do you use ReplaceXTimes
replacing callback implementation from this thread?
It is not quite clear what you mean. Callback is trigerred according to the FindReplaceDirection
specified in FindReplaceOptions
.
Please provide you input and document and sample code you use for testing. We will check the scenario and provide you more information.
Sharing the document.
Replicator Invoice (1).docx (34.2 KB)
In this document, I am trying to replace “1”. It fails when I use the ReplaceXTimes callback. But works when I remove it.
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.setDirection(FindReplaceDirection.FORWARD);
findReplaceOptions.setMatchCase(true);
findReplaceOptions.setFindWholeWordsOnly(true);
findReplaceOptions.setReplacingCallback(new ReplaceXTimes(1));
document.getRange().replace("1", "replacement", findReplaceOptions);
@gkumar16 Thank you for additional information. I have tested the scenario and it works as expected on my side. Here is my full code:
Document doc = new Document("C:\\Temp\\in.docx");
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.setDirection(FindReplaceDirection.FORWARD);
findReplaceOptions.setMatchCase(true);
findReplaceOptions.setFindWholeWordsOnly(true);
findReplaceOptions.setReplacingCallback(new ReplaceXTimes(1));
int replacementsConunt = doc.getRange().replace("1", "replacement", findReplaceOptions);
System.out.println(replacementsConunt);
doc.save("C:\\Temp\\out.docx");
private static class ReplaceXTimes implements IReplacingCallback
{
public ReplaceXTimes(int replacementAmount)
{
ReplacementAmount = replacementAmount;
TrackCounter = 0;
}
@Override
public int replacing(ReplacingArgs replacingArgs) throws Exception {
if (TrackCounter < ReplacementAmount)
{
TrackCounter++;
return ReplaceAction.REPLACE;
}
return ReplaceAction.STOP;
}
private int TrackCounter;
private int ReplacementAmount;
}
If ReplaceXTimes(1)
is specified, only the first occurance of "1"
is replaced, when it is not specified 2 occurrences are replaced as expected.
@alexey.noskov I am using version 20.11. Maybe this is fixed in the newer versions?
@gkumar16 Yes, it looks like a bug in old version of Aspose.Words. I have checked old version and it seems the problem was resolved in 22.2 version of Aspose.Words (issue WORDSNET-23258). So I would suggest you to update to the latest 23.7 version of Aspose.Words.