Free Support Forum - aspose.com

Please advise me use replace API

I have attached a word document with the query as the content. Please refer and advise me. Thanks in advance.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. The problem is in your regular expression. Please try using this one:

Document doc = new Document("C:\\Temp\\in.doc");

Pattern p = Pattern.compile("<]*>>");

doc.getRange().replace(p, "New Value");

doc.save("C:\\Temp\\out.doc");

Best regards,

Hi,
Thanks for your quick response which works great. However I face another problem. I have two special characters to be used in the regex. I tried

Pattern p = Pattern.compile("«[^>]»");

The characters are special not simple greater than and lesser than symbols. Now its throwing some exception like,

Exception in thread “main” java.lang.UnsupportedOperationException: The match includes one or more special or break characters and cannot be replaced.
at com.aspose.words.acc.a(FindReplace.java:213)
at com.aspose.words.acc.Nn(FindReplace.java:171)
at com.aspose.words.Range.replace(Range.java:123)

Wehreas java returns true for the following statement,
Pattern.matches("«[^>]
»", “«aaa»«bbb»”)

Is there any work around to solve this? Please advise. Thanks in advance.


Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Captured and replacement strings cannot contain special characters. An exception is thrown if a captured or replacement string contain one or more special characters: paragraph break, cell break, section break, field start, field separator, field end, inline picture, drawing object, footnote.

http://www.aspose.com/documentation/java-components/aspose.words-for-java/com/aspose/words/range.html

You should take this in account while building the template.

Also, if you need to fill the document with data, I do not think that find/replace is the best option. I think the best option in this case is Mail Merge:

http://www.aspose.com/documentation/java-components/aspose.words-for-java/simple-mail-merge-explained.html

Best regards.

This is really good. How to pass the data source as from an XML file or any other object other than a DB Table. In my requirement, we have the result as an XML object which need to execute in a mail merge with region.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think, you can use IMailMergeDatasource to achieve what you need:

http://www.aspose.com/documentation/java-components/aspose.words-for-java/com/aspose/words/imailmergedatasource.html

Hope this help. Please let me know if you need more information, I will be glad to help you.

Best regards,