Find Keywords in Word Document & Replace with new Content (Text Images etc) using Java | Pattern.compile

Document doc = new Document(“d:/a/word.doc”);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
//i need instance of LayoutEnumerator to do some other job,so i created,but this cause replacement no //working
FindReplaceOptions options = new FindReplaceOptions();
options.setDirection(FindReplaceDirection.FORWARD);
options.setReplacingCallback(new IReplacingCallback() {
@Override
public int replacing(ReplacingArgs target) throws Exception {
target.setReplacement(“abc”);
return 0;
}
});
doc.getRange().replace(Pattern.compile("\$(.?)\{(.*?)\}"), “”, options);

doc.save(“d:/a/fetal3.pdf”, SaveFormat.PDF);

Document doc = new Document(“d:/a/fetal3.doc”);

LayoutEnumerator enumerator = new LayoutEnumerator(doc);
//i need instance of LayoutEnumerator,so i created it ,but this seem to cause replacement failure
//if i do not construct LayoutEnumerator,replacement work as expected
FindReplaceOptions options = new FindReplaceOptions();
options.setDirection(FindReplaceDirection.FORWARD);
options.setReplacingCallback(new IReplacingCallback() {
@Override
public int replacing(ReplacingArgs target) throws Exception {
target.setReplacement(“abc”);
return 0;
}
});
doc.getRange().replace(Pattern.compile("\$(.?)\{(.*?)\}"), “”, options);
doc.save(“d:/a/fetal3.pdf”, SaveFormat.PDF);

@liu9527,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words for Java 20.5 generated output DOCX/PDF file showing the undesired behavior
  • Your expected DOCX/PDF file showing the desired output. You can create this document by using MS Word.
  • Please also create a standalone simple Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you more information. Thanks for your cooperation.

due to my carelessness,i check my code,this situation don’t exist
thank for your patience

@liu9527,

It is great that you were able to find what you were looking for. Please let us know any time you may have any more queries in future.