How to update dynamic value to mergedfield (If condition )

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

@akondewar You can simply remover tables with a single row. The region in your template occupies the second row, which is removed upon executing mail merge. So you can use code like this to remove a lonely row tables:

for(Table t : (Iterable<Table>)doc.getChildNodes(NodeType.TABLE, true))
{
    if(t.getRows().getCount()==1)
        t.remove();
}

@alexey.noskov
Thanks now table empty rows and table labels removed.

Some types of bullet points are not being correctly converted from word to pdf

here I have attached input word template file and sample source code.

Please provide the sample code

TXN-1266.zip (66.1 KB)

@akondewar To render bullet symbol MS Word as well as Aspose.Words uses Symbol font. It looks like the font is not available in the environment where the document is converted to PDF.
To build an accurate document layout the fonts are required. If Aspose.Words cannot find the fonts used in the document the fonts are substituted . This might lead into the layout and appearance difference. You can implement IWarningCallback to get a notification when font substitution is performed.
The following articles can be useful for you:
https://docs.aspose.com/words/java/specify-truetype-fonts-location/
https://docs.aspose.com/words/java/install-truetype-fonts-on-linux/
Please try either installing or providing the fonts required to render the document. This should resolve the problem.

@alexey.noskov
Thanks for the solution, but it’s skip the empty table Label but also not printed next table values.

For example :
SOAckDetails details have data or not it’s skip / not print the 'SOACK_Sub_Total ‘SOACK_Shipping’ etc.

This section data not printed

TestTXN1256.zip (73.7 KB)

@akondewar As I can see fields are properly filled:

Document doc = new Document("C:\\Temp\\in.docx");

String[] names = new String[] {"SOACK_Sub_Total", "SOACK_Shipping", "SOACK_Tax", "SOACK_Total"};
String[] values = new String[] {"Sub_Total", "Shipping", "Tax", "Total"};
doc.getMailMerge().execute(names, values);
        
doc.save("C:\\Temp\\out.docx");

Hi @alexey.noskov
We are using multiple custom templates, we never know the Mergefeild Table name. So we can not used hardcore MergeField Table name in our code. It is our common code.
Please provide generic solution that will not impact on existing templates.