how to use Aspose to conditionally show a table and hide another table in word template?

I have a word template with two table, I want to use Aspose to show a table and hide another table in word template based on a variable in java code, how to achieve this?

Answer

You can easily achieve what you need using IF field in your MS Word template document. https://support.microsoft.com/en-us/office/field-codes-if-field-9f79e82f-e53b-4ff5-9d2c-ae3b22b7eb5e in the condition you can insert a merge field or a bookmark and then update the condition upon execution mail merge or setting bookmark value using Aspose.Words. For example, see the screenshot of the template document and the code to execute mail merge using Aspose.Words. enter image description here

Document doc = new Document("C:\Temp\in.docx");
doc.getMailMerge().execute(new String[] { "test" }, new String[] { "first" });
doc.save("C:\Temp\out.docx");

If the output format is supposed to be MS Word document, you can also call Document.unlinkFields() method before saving, in this case IF field will be removed from the document and only the result will be preserved.