Replace text having special chars with some text


I have attached,

I wrote above document which is having conditional display of data based on the condition. I am able to find the conditional text in my document and able to check condition, I am at stage where I no which IF block to show but when I call ‘replace method’ of Aspose, it throws ‘Special chars, line break not allowed.’

Is there any way by which I can show IF block text and hide other block which does not satisfy condition.
My code is below.

sectionDocument.getRange().replace(conditionalPlaceholder.getConditionalPlaceholder(), conditionalPlaceholder.getText(), false, false);

Where conditionalPlaceholder.getConditionalPlaceholder() means whole IF block.

conditionalPlaceholder.getText() means only text content of IF block that is “The blinded line listings of SARs received during the reporting period are provided in Appendix 4.1”

Vaibhav (31.9 KB)


It is easy to achieve this if you have real IF fields in your document. For example, I have modified your Word document and placed real merge fields and IF fields in it (see (32.3 KB)). In this case, you can use the following simple code to show/hide content of one or other IF block. Hope, this helps.

Document doc = new Document("D:\\temp\\Line_Listings_SARs_During_Reporting_Period-modified.docx");

doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.Execute(new string[] { "condition" },
                    new object[] { "SARDATANOTEXISTS" /*SARDATANOTEXISTS*/ });


Hi Hafeez,

Thanks for your reply!!

Business don’t want to use Mergefield word functionality. They want to add some custom tag like as I mentioned in my word doc. So kindly tell me the solution to achieve this by using custom tag.

Vaibhav Diwan


Please share your current source code. It would be great if you please create a standalone Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.