@akondewar Sure, you can simply use mergefield in the IF field condition. Field code should look like this: {IF "{ MERGEFIELD isBarCode }" = "true" "here is TRUE value" "here is FALSE value" }
Also, in your case you can use MERGEBARCODE field instead of DISPLAYBARCODE. Something like this: { MERGEBARCODE test QR }
Upon executing mail merge MERGEBARCODE field is converted to DISPLAYBARCODE field with the corresponding value.
Is it possible to set the value of ‘isBarCode’ dynamically if it is not present in the input XML file? Then how? Please provide sample code for the same.
@akondewar The problem occurs because isBarcode field in the details section is in the region so it is not filled when simple mail merge is executed. Since upon executing mail merge with regions REMOVE_CONTAINING_FIELDS and REMOVE_UNUSED_FIELDS clean up options are enabled, IF and unfilled isBarcode are removed. In your case you should modify the code like this:
Document doc = new Document("C:\\Temp\\in.docx");
DataSet dataSet = new DataSet();
dataSet.readXml("C:\\Temp\\data.xml");
int cleanupOptions = MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS |
MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS;
doc.getMailMerge().setCleanupOptions(cleanupOptions);
doc.getMailMerge().executeWithRegions(dataSet);
cleanupOptions |= MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS;
cleanupOptions |= MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS;
doc.getMailMerge().setCleanupOptions(cleanupOptions);
String strIsBarcode = "true";
if (strIsBarcode != "")
{
doc.getMailMerge().execute(new String[] { "isBarcode" }, new Object[] { strIsBarcode });
}
doc.save("C:\\Temp\\out.docx");
Hi @alexey.noskov
We can not remove doc.getMailMerge().deleteFields(); and doc.updateFields(); before save. I encountered the following issues, which I posted on the forum.
@akondewar Please try saving the output document as DOCX and make sure the DISPLAYBARCODE field is there in the output document. Also, please try setting barcode generator before executing mail merge:
@alexey.noskov
Hi ,
While using suggested code, we are facing another issue that is, at least one character is required before "{IF {MERGEFIELD displaybarcode }="true" {DISPLAYBARCODE "{MERGEFIELD WOBarcode \*MERGEFORMAT }" s:CODE128-w:13\h 300\t\* MERGEFORMAT}" {MERGEFIELD WOBarcode \*MERGEFORMAT}}" in the header section.
If we removed character before above condition then Barcode is not printed.
@akondewar It is required to accurately format the fields in the template to get the correct result. There must be whitespaces before and after condition operator in the IF field, there must be whitespaces between true and false values. If the values in the IF field have whitespaces they must be enclosed into double quotes.
If the field is formatted improperly, it cannot be parsed properly by both MS Word and Aspose.Words. You can save the output as MS Word document to make sure the resulting field is formatted properly and can be properly updated by MS Word.
@akondewar I see that your document has bad formatted IF fields. I have created a simplified bad input document: bad.docx (27.0 KB) bad_out.docx (21.9 KB)
The problem can be reproduced using the following code:
Document doc = new Document("C:\\Temp\\bad.docx");
doc.getMailMerge().deleteFields();
doc.updateFields();
doc.save("C:\\Temp\\out.docx");
If field should always contain true and false values, in your case false value is missed. Here is the corrected document: in.docx (27.0 KB) out.docx (21.9 KB)
@akondewar In your code there is no code that removes the the empty region table title row. The same there is not IF field condition in your template the will hide it. So it is expected that the table header remains in the output.