Use Nested Mail Merge Field inside IF Field to Conditionally Show Hide Table in Word DOCX Document using JAVA

@amitpatelbnft,

Regarding WORDSNET-20477, we have made a few changes to the code; please check on your end and let us know if the following code changes (mRecordIndex = 0, updateFields()) are acceptable for you?

public void SimpleTest() throws Exception
{
    test("testTemplateWithIF.docx");
    test("testTemplateWithoutIF.docx");
}

private void test(String input) throws Exception
{
    String inputFile = "X:\\tmp\\WORDSNET-20477\\" + input;
    Document baseDoc = new Document(inputFile);
    baseDoc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
    baseDoc.getMailMerge().setMergeDuplicateRegions(false);

    mergeDataToDocument(baseDoc, buildUserHeaders(), "userTableHeader");
    mergeDataToDocument(baseDoc, buildUsers(), "usersTable");
    mergeDataToDocument(baseDoc, buildUsers(), "childTable");

    baseDoc.updateFields();//!! added
    baseDoc.save(inputFile + ".out.0.index.with.update.docx");
}

public TestMapDataSource(List<Map<String, Object>> mapList, String tableName)
{
    this.mapList = mapList;
    this.tableName = tableName;
    mRecordIndex = 0;//!! was -1
}

@awais.hafeez
We tried this suggestion. It will not work for us. We have the new issues in case we change mRecordIndex to zero.

Currently we are using examples from Aspose documentation and mRecordIndex is -1 in this example.

The method getChildDataSource will be called if we remove “IF” condition. getChildDataSource is called inside this encrypted class zzZH7 → zz03.zz6(var1) → this.zzXV(0, 1) → getChildDataSource. We are not sure if it help because we have only encrypted classes but if we have IF condition, the method getChildDataSource will not be called inside this.zzXV(0, 1).

@amitpatelbnft,

We have logged these details in our issue tracking system. We will inform you via this forum thread as soon as this issue will get resolved in future or any more updates may be available. We apologize for any inconvenience.

@awais.hafeez Do you have ETA on this? Thanks!

@amitpatelbnft,

WORDSNET-20477 is currently ‘in analysis’ phase and there is no ETA available at the moment. We may be able to calculate and share the ETA (time frame) with you after completing the analysis of this issue. Sorry for the inconvenience.

@awais.hafeez Any new update on this one yet? Thanks!

@amitpatelbnft,

I am afraid, WORDSNET-20477 is not resolved yet and there is no further news about this issue i.e. the issue is still “in analysis” phase. We will inform you here when this issue will get resolved in future or any more updates may be available. We apologize for the inconvenience.

@awais.hafeez any new update on this issue? Thanks!

@amitpatelbnft,

Unfortunately, your issue is still unresolved. We have not completed the analysis of this issue yet. We will keep you posted here on any further updates and notify you when this will get resolved in future. We apologize for the inconvenience.

@awais.hafeez any new update yet? Thanks!

@amitpatelbnft,

WORDSNET-20477 is still in Analysis stage. Please spare us some time for the investigation of this issue. We will get back to you with our findings soon.

@amitpatelbnft,

I am afraid, we are unable to fully understand what your requirement actually is. We also need a sample of result (expected) document to understand what exactly you are trying to achieve. Please compose in MS Word the full output document with manually filled headers, sub-headers and all the fields. ZIP the DOCX file showing the desired output and attach it here for further testing. Thanks for your cooperation.

I have already shared a sample program in this chat with details so not sure what else you want us to provide.

@amitpatelbnft,

Can you please also provide a Word DOCX document that shows us the desired output? You can create this document manually by using MS Word or any other editor. This will help us to understand the structure of your expected document as to how you want your final output be generated like.

The AsposeTestProject.zip I have attached in above comment has requested files too.

@amitpatelbnft,

The “AsposeTestProject.zip” that you attached in this post earlier has four files in this “~\AsposeTestProject\Examples\src\main\resources\com\aspose\words\examples\loading_saving\TestAsposeWordToPdf” location and six files in this “~\AsposeTestProject\Examples\target\classes\com\aspose\words\examples\loading_saving\TestAsposeWordToPdf” location. Can you please clarify a bit which of these files should we take as an expected file?

It would be great if you please provide again a Word DOCX document that shows us the desired output (that you want Aspose.Words to produce)? You can create this document manually by using MS Word or any other editor. This will help us to understand the structure of your expected document. Thanks for your cooperation.

@awais.hafeez As I mentioned in the comment, it seems there is a bug in Aspose.word with getChildDataSource and “if statement”.It looks like getChildDataSource() is not calling correctly when we have “if statement”. In the AsposeTestProject.zip, we have testTemplateWithIF.docx which has “if statement” around getChildDataSource. Ideally it should render correct output into the “testTemplateResultWithIF.pdf” as an output when you run the AsposeTestProject project but it rendering as below which is incorrect,
image.png (175.3 KB)

Correct output should look like as below,
image.png (36.5 KB)

@amitpatelbnft,

We have logged these details in our issue tracking system and will keep you posted here on further updates.

@amitpatelbnft,

During further investigation we had determined that the template you shared has some problems. All you need to do is to surround the tables used as “true” arguments of the IF fields with double quotes. Otherwise MS Word and Aspose.Words do not understand that argument correctly. Please find attached fixed template and output DOCX and PDF files:

The issues you have found earlier (filed as WORDSNET-20477) have been fixed in this Aspose.Words for .NET 21.2 update and this Aspose.Words for Java 21.2 update.