We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

IF condition in Template (Mail Merge Mustache Syntax) is not working

HI

We tried to create a template with below structure and data is not pumping to this template?
Is this correct syntax?

could you please provide some update on this?

Hi Kiran,

Thanks for your inquiry. Could you please ZIP and attach your template document and source code (simple Java application) here for testing? We will investigate the issue on our end and provide you more information.

Best regards,

Hi Hafeez,

Thanks for the reply, ‘if condition’ issue is fixed , i followed below if syntax its working fine now.
I used Mailmerge field 'if ’ syntax like click on ctl+F9 and formed a condition like this
{IF {{ELEMENT_CLASSIFICATION}} = “Earnings” “” “”}


But i got one more issue this above if condition is under foreach loop.
for each looping it inserts empty row, how to remove that empty row.


Please find the attachment with java code

Hi Kiran,


Thanks for your inquiry. Please try using the following code:
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document doc = <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>new <span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document(<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>“D:<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>temp<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>GB Payroll Register.rtf”<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>);
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
DataSet ds = new DataSet();
ds.readXml(“D:\temp\213_grouped_GB Payroll Register.xml”);

doc.getMailMerge().setUseNonMergeFields(true);
doc.getMailMerge().setCleanupOptions(
MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS |
MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS |
MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS |
MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS);

doc.getMailMerge().executeWithRegions(ds);

Table table = doc.getFirstSection().getBody().getTables().get(1);
Cell firstCell = table.getFirstRow().getFirstCell();
for (Paragraph para : (Iterable) firstCell.getChildNodes(NodeType.PARAGRAPH, false)) {
if (para.toString(SaveFormat.TEXT).trim().equals("")) {
para.remove();
}
}

Cell lastCell = table.getFirstRow().getLastCell();
for (Paragraph para : (Iterable) lastCell.getChildNodes(NodeType.PARAGRAPH, false)) {
if (para.toString(SaveFormat.TEXT).trim().equals("")) {
para.remove();
}
}

doc.save(“D:\temp\awjava-17.1.0.docx”);
doc.save(“D:\temp\awjava-17.1.0.pdf”);

Best regards,

Hi


This worked for now but it would be better to fix the template itself on how to remove the spaces otherwise it looks hardcoding the scenario in java and may not solve the issue in other scenarios

Can you please suggest permanent fix?

how come for each loop in the template it generating another new line?

Thanks
Kiran
Hi Kiran,

Thanks for your inquiry. We are working over your query and will get back to you soon.

Best regards,

Hi Hafeez


We are stand by for your response and we are getting severe presssure from our customer production requirement . Need to resolve this ASAP.

If there is IF condition in the FOR EACH loop wherever it is in the template, we are getting a line
and also for ELSE condition.

This does not look efficient when we are presenting PDF to executives and cannot present also.

Kindly respond to this asap or provide us any alternative for us to move forward.

We bought complete ASPOSE.TOTALS and we are using cells, words, slides also with this.

Thanks
Kiran
Hi Kiran,

Thanks for your inquiry. We tested the scenario and have managed to reproduce the same problem on our end. For the sake of any correction, we have logged this problem in our issue tracking system as WORDSNET-14875. Our product team will further look into the details of this problem and we will keep you updated on the status of this issue. We apologize for your inconvenience.

Best regards,

Hi Hafeez


Thank you !
Could you please provide update on when we can expect this fix?

Thank you
Kiran Pasham

Hi Kiran,


Thanks for your inquiry. Unfortunately, your issue is not resolved yet. Our product team has completed the analysis and the root cause of this issue has been identified. We will inform you via this thread as soon as this issue is resolved. We apologize for your inconvenience.

In the meantime, while you’re waiting for a fix, we suggest you please write your own solution for this problem. Probably, based on following code snippet (output PDF is attached):

<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document doc = new Document(“D:\temp\GB Payroll Register.rtf”);

DataSet ds = new DataSet();
ds.readXml(“D:\Temp\213_grouped_GB Payroll Register.xml”);

doc.getMailMerge().setUseNonMergeFields(true);

doc.getMailMerge().setCleanupOptions(
MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS |
MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS |
MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS |
MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS);

CellsCollector cellsCollector = new CellsCollector();
doc.getMailMerge().setFieldMergingCallback(cellsCollector);
doc.getMailMerge().executeWithRegions(ds);
cellsCollector.clearCells();

doc.save(“D:\temp\awjava-17.2.0.pdf”);


<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>static class CellsCollector implements IFieldMergingCallback {
private ArrayList cells = new ArrayList();

public void fieldMerging(FieldMergingArgs args) {
Cell cell = (Cell) args.getField().getStart().getParentParagraph().getParentNode();
if (cell == null)
return;

if (cells.contains(cell))
return;

cells.add(cell);
}

public void imageFieldMerging(ImageFieldMergingArgs args) {
}

public void clearCells() {
for (Cell cell : cells) {
boolean removeParagraphs = anyTables(cell) && onlyTablesAndEmptyParagraph(cell);

if (removeParagraphs) {
ArrayList paragraphsToRemove = new ArrayList();
for (Node node : (Iterable) cell.getChildNodes())
if (node.getNodeType() == NodeType.PARAGRAPH)
paragraphsToRemove.add((Paragraph) node);

for (Paragraph paragraph : paragraphsToRemove)
paragraph.remove();
}
}
}

private boolean anyTables(Cell cell) {
for (Node node : (Iterable) cell.getChildNodes()) {
if (node.getNodeType() == NodeType.TABLE)
return true;
}

return false;
}

private boolean onlyTablesAndEmptyParagraph(Cell cell) {
for (Node node : (Iterable) cell.getChildNodes()) {
if (!isTableOrEmptyPara(node))
return false;
}

return true;
}


private boolean isTableOrEmptyPara(Node node) {
return (node.getNodeType() == NodeType.TABLE) ||
(node.getNodeType() == NodeType.PARAGRAPH && isNullOrWhiteSpace(node.getText().trim()));
}

private boolean isNullOrWhiteSpace(String str) {
boolean flag = false;

if (str == null || str == “”)
flag = true;

if (str.trim().length() == 0)
flag = true;

return flag;
}
}

Hope, this helps.

Best regards,

Hi Kiran,


It is to update you that after further analysis our product team came to know that they might not be able to provide a fix in Aspose.Words for Java API itself because the implementation of this fix would introduce breaking changes in Aspose.Words’ API which they would like to avoid.

Therefore, please test the workaround code provided in my previous post and let us know if this solution is acceptable for you? Thanks for your cooperation and understanding.

Best regards,

A post was split to a new topic: Show data based on condition