We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

More than one IF clause it fails with String index out of range: -38

Hi All,

We have using Aspose.Words.jdk16-14.1 jar and IReplacingCallback() { replacing(ReplacingArgs) for replacing the content of IF clause merge field with following word template/mail-merge field texts:

Above operation works fine incase we have only one IF clause above but in case we have more than one as the case above it fails with “String index out of range: -38”.

Following is the sample Java code:
Pattern regexp2 = Pattern
try {
IReplacingCallback a;
doc.getRange().replace(regexp2, new IReplacingCallback() {

public int replacing(ReplacingArgs arg0) throws Exception {
Matcher matcher2 = arg0.getMatch();
String text1 = “”;
String text2 = “”;
text1 = matcher2.group(0);
text2 = “”;
if (matcher2.group(3).equals("=")) {

if (matcher2.group(2).equals(matcher2.group(4)))
text2 = matcher2.group(5);
if (matcher2.group(3).equals("<>")) {
if (!matcher2.group(2).equals(matcher2.group(4)))
text2 = matcher2.group(5);
if (text1.contains("\r"))
throw new IpasException(
Messages.getString(“msg.MailmergePostProcessingTheIFClauseContainedInTemplateFileHasNewParagraphsCharactersUseLineBreakInstead”) + text1);
if (text1.contains(“MERGEFIELD”)) {
.println(“MERGEFIELD contained in IF clause.” + text1);
return 1;
try {
} catch (Exception e) {
.println(“Postprocessing error. Replacing text in:” + text2);

// TODO Auto-generated catch block

return 0;

}, true);
} catch (Exception e1) {}

Similar code worked for us to override the "ReplaceEvaluator-> replace(Object arg0, ReplaceEvaluatorArgs arg1){…} with old version of Aspose "Aspose.Words.jdk15-2.4.2 jar"

Any input is very help ful.

Thank you


I have observed the issue shared by you and it seems to be an issue related to Aspose.Words. I am moving this thread to Aspose.Words forum where our respective support team will assist you further in this regard.

Many Thanks,

Thank you Mudassir for your reply.

Just to add to the issue details.

In case my code updated by replacing the second parameter of the replace method to ‘false’, functionality works and don’t throw any exception.
doc.getRange().replace(regexp2, new IReplacingCallback(), false){

So, my question is what is the usage of second parameter 'false' means and what will be its impact on generated document with my mail-merge template?

Many thanks for your kind attention and assistance.


Hi Nilesh,

Thanks for your inquiry. This issue does not occur if you use backward evaluation instead of forwards in your code. This is just a matter of changing the third parameter of the replace method to false. Generally, it is recommend if you are modifying the document in a custom replacement evaluator then you should use backward replacement.

Best regards,

Thank you Hafeez for response.

Please let me know when you will use third parameter as true with replace method as in this case recommended option is to use false.

So, does it means it will be always false with replace method? As in case it is true, this will fail with exception discussed here.


Hi Nilesh,

Thanks for your inquiry. We are in coordination with product team to get answer pertaining to your queries. Soon you will be updated with the required information.

Best regards,

Hi Nilesh,

The isForward parameter indicates whether the find and replace will be performed in forward or backward direction inside the Root Node. Default is false!
We need this parameter only upon using custom ReplaceEvaluator, to avoid problem with node indexes. So you have to use False (Default) if you are going to change nodes in any way using ReplaceEvaluator.

You can use True if you need to replace for example only the first occurrence of searched word.

Hope, this helps.

Best regards,