How to update dynamic value to mergedfield (If condition )

@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.

Thanks for reply.
I have another question,

{IF {MERGEFIELD isBarCode ="true"} {DISPLAYBARCODE "{MERGEFIELD WOBarcode \* MERGEFORMAT }" s:CODE128-w:13\h 300\t\* MERGEFORMAT } {MERGEFIELD WOBarcode \* MERGEFORMAT } }

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 You can simply execute mail merge with the required field. For example using code like this:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().execute(new String[] { "isBarCode" }, new Object[] { "true" });

Hi @alexey.noskov
I am using provide sample code, it’s working fine in header section, but does not work for details section.

Condition : Working fine is headed section

{IF {MERGEFIELD isBarcode}="true" {DISPLAYBARCODE "{MERGEFIELD WOBarcode \*MERGEFORMAT }" s:CODE128-w:13\h 300\t\* MERGEFORMAT}" {MERGEFIELD WOBarcode \*MERGEFORMAT}}

Detail section :
{IF {MERGEFIELD isBarcode}="true" {DISPLAYBARCODE "{MERGEFIELD ItemBarcode \*MERGEFORMAT }" s:CODE128-w:13\h 300\t\* MERGEFORMAT}" {MERGEFIELD Line \*MERGEFORMAT}}

Input xml, sample code and input word template is attached.

Forum.zip (87.5 KB)

@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.

https://forum.aspose.com/t/java-lang-nullpointerexception-while-using-page-mergeformat-numpages-mergeformat-tag-in-word-template-file-when-row-remove-is-called-before-document-save/265912?u=akondewar

https://forum.aspose.com/t/when-using-if-condition-in-a-word-template-characters-starting-with-a-double-quote-do-not-appear-in-the-pdf/268188/6

Even I have added suggested code but did not works.

TXN-182.zip (66.1 KB)

@akondewar You should move the following code upper in your code:

String strIsBarcode = "true";
if (strIsBarcode != "")
{
    doc.getMailMerge().execute(new String[] { "isBarcode" }, new Object[] { strIsBarcode });
}

Exactly after:

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);		

In the code you have provided the code is executed after deleting all merge fields, so it does not have any effect.

After this change header barcode value also not printed.

@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:

doc.getFieldOptions().setBarcodeGenerator(new BarcodeGenerator());

Hi @alexey.noskov
We are generating output is as a pdf , not a docx.
There are hundreds of live customers using pdf output instead of docx.

Kindly provide the solution.

@akondewar I understand that your target format is PDF. I mean save the document as DOCX for testing and analysis purposes.

Please try modifying your code like this:

merge = doc.getMailMerge();
// Merge unconditional MergeFields and Regions
merge.setUnconditionalMergeFieldsAndRegions(true);

// Reading the data from xml file for the mail merge
DataSet dataSet = new DataSet();
dataSet.readXml(strPath + "data.xml");

int cleanupOptions = MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS |
        MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS;

merge.setCleanupOptions(cleanupOptions);
merge.executeWithRegions(dataSet);

// merge the header fields
DataTableCollection tables = dataSet.getTables();
if (tables != null && tables.getCount() > 0)
    merge.execute(tables.get("PrintForm"));

cleanupOptions |= MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS;
cleanupOptions |= MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS;
merge.setCleanupOptions(cleanupOptions);

String strIsBarcode = "true";
if (strIsBarcode != "")
    merge.execute(new String[] { "isBarcode" }, new Object[] { strIsBarcode });

@alexey.noskov
Thanks for the solution, we would like to know is there any regression on following issues

*1. How to update dynamic value to mergedfield (If condition ) - #13 by akondewar

2. When using 'IF' condition in a word template, characters starting with a double quote do not appear in the PDF - #6 by alexey.noskov

@akondewar There should not be any problems with the modified code. But it would be better to test on your side with your real data and templates.

@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.

Sample code and genereated files attached.
22-Feb-2024_ToForum.zip (141.0 KB)

@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.

@alexey.noskov
We implemented provided solution , but empty “” and white spaces printed in generated pdf.

Here I have attached sample code, input xml file, word template file and generated pdf file.

01-Oct-2024.zip (131.0 KB)

@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)

@alexey.noskov Thanks,

We are getting regression impact, if table don’t have records then table header are printed.

Example : Sales Order dont have line records but Lines Table ('SOAckDetails ') header printed.

Here is sample code
TXN-1256.zip (77.7 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.

@alexey.noskov
Kindly share the sample code for removes the empty region table title.