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

Free Support Forum - aspose.com

Search & replace text of doc

I am working on a requirement where I have to replace text in multiple lines of doc file with the values coming from xml.
The pattern for replacement is that the string between < > is to be replaced with the value from xml.
The string inside < > can have different values but starting with “field”.
For example,
Illustration reference:
Expiry date:
So, I have to replace with some value say abc123.
and with say 14/01/2009
so that my modified word document is some what like …
Output doc :
Illustration reference: abc123
Expiry date: 14/01/2009
Can you please guide me on this?

Thanks for your request. You can try using the following code:

public void Test081()
    //Open document
    Document doc = new Document(@"Test081\in.doc");
    //Create regular exapression that will find fields
    //define constant parts of field
    string start = Regex.Escape(" string format = Regex.Escape("format = ");
    string end = Regex.Escape(">");
    string quote = Regex.Escape(""");
    string regexStr = start + quote + @"(?[^ \t\n\r\f\v>]+)" + quote +
    @"\s?(" + format + quote + "(?[^ \t\n\r\f\v>]+)" + quote + ")?" + end;
    Regex regex = new Regex(regexStr);
    doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceTags), false);
    //Save output document
private ReplaceAction ReplaceTags(object sender, ReplaceEvaluatorArgs e)
    //Get matched values
    string fieldName = e.Match.Groups["name"].Value;
    string fieldFormat = e.Match.Groups["format"].Value;
    string replacement = string.Empty;
    switch (fieldName)
        case "ILLUSTRATION_REF":
            replacement = "this is my replacement";
        case "EXPIRY_DATE":
            replacement = DateTime.Now.ToString(fieldFormat);
    e.Replacement = replacement;
    return ReplaceAction.Replace;

Sample document is attached.
Hope this helps.
Best regards.

Hi Alexey,
Thanks a lot for your reply.
Can you please tell me that is this code meant for Java because I am having problem while importing the proper classes for it.
It is not able to find what is Regex, e.Match (should I use getMatch() instead) & the symbol @ in reg exp.

Just realised I didn’t mention that I am using Java.
Can you please provide the help using Java.
Thanks & apologies,

Thanks for your request. Here is java code:

    public static void main(String[] args) throws Exception {
        // Open the document.
        Document doc = new Document("C:\\Temp\\in.doc");
        //Create regular exapression that will find fields
        //define constant parts of field
        String start = " String format = " format = ";
        String end = ">";
        String quote = "\"";
        String regexStr = start + quote + "([^ \\t\\n\\r\\f\\v>]+)" + quote +
                "\\s?(" + format + quote + "([^ \\t\\n\\r\\f\\v>]+)" + quote + ")?" + end;
        Pattern regex = Pattern.compile(regexStr);
        doc.getRange().replace(regex, new ReplaceTagsEvaluator(), false);
        // Save the modified document.

    public class ReplaceTagsEvaluator implements ReplaceEvaluator {
         * A user implemented ReplaceEvaluator.replace() method is called for each
         * match found during a replace operation.
         * @return An enumerated value that specifies the action to be taken for the current match.
        public int replace(Object sender, ReplaceEvaluatorArgs e) throws Exception {
            //Get matched values
            String fieldName = e.getMatch().group(1);
            String fieldFormat = e.getMatch().group(3);
            String replacement = "";
            if (fieldName.equalsIgnoreCase("ILLUSTRATION_REF")) {
                replacement = "this is my replacement";
            } else if (fieldName.equalsIgnoreCase("EXPIRY_DATE")) {
                java.util.Date date = new java.util.Date();
                java.text.DateFormat dateFormat = new java.text.SimpleDateFormat(fieldFormat);
                replacement = dateFormat.format(date);
            return ReplaceAction.REPLACE;

Hope this helps.
Best regards.

Thanks for the help. Its working now.
Can you please tell me that can we have multiple regex.
I am attaching a doc file. I need to replace the data in the table as well as I have to evaluate the if conditions to replace data.
Let me know if that can be done too.
Also can you please tell when will Aspose be able to convert Word file into Pdf using Java.

Thanks for your request. Yes, of course, you can use few ReplaceEvaluators or perform Replace several times.
I also would like to advice using Mail Merge feature to fill your template with data. Please see the following links for more information:

Best regards.

Regarding PDF format. Aspose.Words for Java does not support PDF format and document printing yet. This feature will be ported to java version form .NET baseline. Hopefully PDF will be supported in first half of 2009. Unfortunately, I cannot provide you more accurate estimate at the moment.
Best regards.

We are happy to inform you that the first auto-ported version of Aspose.Words for Java is ready. This version supports converting documents to PDF. You can get it from here.