Hello,
We are using aspose mailmerge with java and xml input. We have a requirement where if the table doesn’t have any records, we should not display the header. We are able to achieve this with the following code.
However, we have some tables that are with a single row and the data is populated based on the input file. With the following code it is deleting those tables as they have just one row. Could you please advise how we can achieve this functionality.
In the attached document archiveRcdText is a variable and a section heading in the template. We need that to be displayed when there is value for archived text. The table with the following headings should not be displayed when there is no data in the table. With our current code, it is deleting both tables even though we have value for archive variable and as it is a single row in the table.
Docket Text Archive ID Entry Date Entered By
Is there a better way to achieve this requirement?
JsonParser parser = new JsonParser();
JsonObject jsonInput = parser.parse(json).getAsJsonObject();
DataSet mappingData = new DataSet();
System.out.println("Converting json to XML");
InputStream xmlStream = new ByteArrayInputStream(converttoXML1(jsonInput.get("data").toString()).getBytes());
mappingData.readXml(xmlStream);
try
{
//Document doc = new Document(xmlStream);
Document doc = new Document("C://temp//xml//FamilyCaseJacketSummaryTemplateFN.docx");
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS | MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
| MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS | MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS |
MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS);
doc.getMailMerge().executeWithRegions(mappingData);
doc.getMailMerge().execute(mappingData.getTables().get("mappingdataroot"));
// Remove empty rows from the tables.
Iterable<Table> tables = doc.getChildNodes(NodeType.TABLE, true);
for (Table t : tables)
{
// Consider row as empty if it does not have any RUN nodes.
while (t.getLastRow() != null
&& t.getLastRow().getChildNodes(NodeType.RUN, true).getCount() == 0)
{
t.getLastRow().remove();
}
//If there are no records for a given table, remove the table and also the header from generated PDF
if (t.getRows().getCount() == 1)
t.remove();
}
//doc.save("C://temp//xml//WorkingWithXmlData.NestedMailMerge.pdf");
doc.save("C://temp//xml//FAM.pdf");
}
catch (Exception e)
{
e.printStackTrace();
}
Template.docx (31.9 KB)