Free Support Forum - aspose.com

Conditional text processing based on mailMerge

Didn’t find the answer.
Can I implement conditional text processing based on mailMerge in the latest versions of Aspose.Words for Java?
I mean something like this. In the MSWord template I have the following text:
IF ()
END IF
If the condition is true, the text that follows it (paragraphs, tables, images etc.) is included in the document output. If the condition is false, the no text is added to the output.

The сondition may look like this:
{ MERGEFIELD someValue} > 3
{ MERGEFIELD someValue } = 3
etc.
And, of course someValue can be one of the values of a collection or a nested collection, implemented by IMailMergeDataSource.

Hi Dmitry,


Thanks for your inquiry.

Sure, you can use this technique to insert conditional content into your document. You can use an IF field in your document and insert the appropriate data into the conditional part using a mergefield. Please see the code below and attached input/output documents for an example:
Document doc = new Document(“C:\Temp\test.docx”);
doc.getMailMerge().execute(new String[]{“field”}, new Object[]{“hide”});
doc.save(“C:\Temp\out.docx”);
Please note that Aspose.Words will leave the IF field in your document even after merging. You can use the code from here to convert such fields to static text.

If I can help you with anything else, please feel free to ask.

Best regards,