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

Free Support Forum - aspose.com

Incorrect Find&Replace may be problem with DocumentBuilder

Dear sirs!

I tryed to change any text string to URL by Find&Replace method. It’s strange, but text I searched is simply being removed.

My code is here
public int replacing(ReplacingArgs args) {
String matchStr = args.getMatch().group();
try{
if (!matchStr.equalsIgnoreCase(exclude)){//dont replace the same document link
String url = ret.getURL(matchStr); //create URL, it’s work - it’s not empty string
if (url==null) return ReplaceAction.SKIP; //URL doesn’t exist
Run found = (Run)args.getMatchNode();
Run run2 = (Run)found.deepClone(true);
int index = found.getText().indexOf(args.getMatch().group()); //position of sersching text
if (index!=-1){//… I found text to replace
//clip text was found
run2.setText(found.getText().substring(index + args.getMatch().group().length()));
found.setText(found.getText().substring(0, index));
//now I have source text without words that I should replaced,
//run2 contains tail of string (after words that I should replaced
found.getParentParagraph().insertAfter(run2, found); //в исходном тексте вставляем run2 (хвост) после текста, который идет до заменяемого блока.Т.е. кусок без заменяемого текста
//сделано
DocumentBuilder builder = new DocumentBuilder();
builder.setDocument((Document) args.getMatchNode().getDocument());
//Move to mathed node
builder.moveTo(run2);//move cursor to insert - may be error is here
builder.getFont().setUnderline(Underline.SINGLE);
builder.getFont().setColor(Color.BLUE);
//Insert hyperlink
builder.insertHyperlink(matchStr, url, true);//…
}
return ReplaceAction.REPLACE;
}else
return ReplaceAction.SKIP;
}catch (Exception e){
e.printStackTrace();
return ReplaceAction.REPLACE;
}
}

In my doc file I should replace words starts as “СТП”, file repacing.docx contains result of job

Because i’m stuppid :-). Not ReplaceAction.REPLACE! Should be ReplaceAction.SKIP, because I made all replacment yet.

Hi Boris,


Thanks for your inquiry. It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,