How to toggle data in the word template

We would like to know how to handle following scenario in word template

Scenario : Would like to print only 'isExclude = false 'data in ‘SOAckDetails’ and print total of printed lines.

Input xml :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<PrintForm>
	<SOACK_Total>224.000000</SOACK_Total>
	<Order_Number>102235</Order_Number>
	<SOAckDetails>
		<isExclude>true</isExclude>
		<Line>1</Line>
		<Product_Description>HP</Product_Description>
		<Product_Unit_Price>5.000000</Product_Unit_Price>
		<Firmed>1.000000</Firmed>
	</SOAckDetails>
	<SOAckDetails>
		<isExclude>false</isExclude>
		<Line>2</Line>
		<Product_Description>DELL</Product_Description>
		<Product_Unit_Price>6.000000</Product_Unit_Price>
		<Firmed>1.000000</Firmed>
	</SOAckDetails>
	<SOAckDetails>
		<isExclude>true</isExclude>
		<Line>3</Line>
		<Product_Description>Apple</Product_Description>
		<Product_Unit_Price>7.000000</Product_Unit_Price>
		<Firmed>1.000000</Firmed>
	</SOAckDetails>
	<SOAckDetails>
		<isExclude>false</isExclude>
		<Line>4</Line>
		<Product_Description>Accer</Product_Description>
		<Product_Unit_Price>8.000000</Product_Unit_Price>
		<Firmed>1.000000</Firmed>
	</SOAckDetails>
	<SOAckDetails>
		<isExclude>false</isExclude>
		<Line>5</Line>
		<Product_Description>Thinkpad</Product_Description>
		<Product_Unit_Price>198.000000</Product_Unit_Price>
		<Firmed>1.000000</Firmed>
	</SOAckDetails>
</PrintForm>

Word Template :
SO Acknowledgemen.docx (19.2 KB)

@akondewar You can use SKIPIF field to skip entries where isExclude is true. Just put the following field after the TableStart field:

{ IF "{ MERGEFIELD isExclude }" = "true" }

See the following modified template: in.docx (19.4 KB)

DataSet ds = new DataSet();
ds.readXml("C:\\Temp\\data.xml");
        
Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().executeWithRegions(ds);
doc.save("C:\\Temp\\out.docx");

out.docx (14.4 KB)

Alternatively you can filter data before passing them to Aspose.Words.

@alexey.noskov
Thanks for reply.

We have one more scenario, would like to skip the lines if isExclude =“true” or closedshort = “true” or both are true. So kindly let us know how to handled at word template level?

@alexey.noskov 
Thanks for reply.

We have one more scenario,  would like to skip the lines if isExclude ="true" or closedshort = "true" or both are true. So kindly let us know how to handled at word template level?
```xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<PrintForm>
	<SOACK_Total>224.000000</SOACK_Total>
	<Order_Number>102235</Order_Number>
	<SOAckDetails>
		<isExclude>true</isExclude>
		<closedshort>false</closedshort>
		<Line>1</Line>
		<Product>Force1</Product>
		<Product_Description>HP</Product_Description>
		<Product_Unit_Price>5.000000</Product_Unit_Price>
		<Firmed>1.000000</Firmed>
	</SOAckDetails>
	<SOAckDetails>
		<isExclude>false</isExclude>
		<closedshort>false</closedshort>
		<Line>2</Line>
		<Product>Force2</Product>
		<Product_Description>DELL</Product_Description>
		<Product_Unit_Price>6.000000</Product_Unit_Price>
		<Firmed>1.000000</Firmed>
	</SOAckDetails>
	<SOAckDetails>
		<isExclude>false</isExclude>
		<closedshort>true</closedshort>
		<Line>3</Line>
		<Product>Force3</Product> 
		<Product_Description>Apple</Product_Description>
		<Product_Unit_Price>7.000000</Product_Unit_Price>
		<Firmed>1.000000</Firmed>
	</SOAckDetails>
	<SOAckDetails>
		<isExclude>false</isExclude>
		<closedshort>false</closedshort>
		<Line>4</Line>
		<Product>Force4</Product>
		<Product_Description>Accer</Product_Description>
		<Product_Unit_Price>8.000000</Product_Unit_Price>
		<Firmed>1.000000</Firmed>
	</SOAckDetails>
	<SOAckDetails>
		<isExclude>false</isExclude>
		<closedshort>true</closedshort>
		<Line>5</Line>
		<Product>Force5</Product>
		<Product_Description>Thinkpad</Product_Description>
		<Product_Unit_Price>198.000000</Product_Unit_Price>
		<Firmed>1.000000</Firmed>
	</SOAckDetails>
</PrintForm>

Word template :
SO Acknowledgement101.docx (19.7 KB)

Word template :
SO Acknowledgement101.docx (19.7 KB)

1 Like

@akondewar To implement multiple conditions in SKIPIF field, you can use COMPARE field.
in your case field code should look like this:

{ SKIPIF { = OR ({ COMPARE { MERGEFIELD isExclude } = true }, { COMPARE { MERGEFIELD closedshort } = true }) } = 1 }

@alexey.noskov ,
Thanks for reply , I have added suggested condition in template { SKIPIF { = OR ({ COMPARE { MERGEFIELD isExclude } = true }, { COMPARE { MERGEFIELD closedshort } = true }) } = 1 }

but all data printed in generated pdf file. So I am sharing with you input xml, word template , sample code and generated output pdf file.

Kindly do the needful.

Forum.zip (66.5 KB)

@akondewar There must be whitespaces between the first expression, operator and the second expression in the conditions.
This will not work

{ COMPARE { MERGEFIELD isExclude }=true }

But this will:

{ COMPARE { MERGEFIELD isExclude } = true }

Please see the modified template: out.docx (14.7 KB)

Hi, still dose not work, document is attached follows
SO Acknowledgement102.docx (20.0 KB)

Input xml and sample code already provided in previous reply

@akondewar Your modified template works as expected on my side. I have used the following simplified code for testing:

DataSet ds = new DataSet();
ds.readXml("C:\\Temp\\data.xml");
        
Document doc = new Document("C:\\Temp\\SO Acknowledgement102.docx");
doc.getMailMerge().executeWithRegions(ds);
doc.save("C:\\Temp\\out.docx");

Here is the template i have modified myself:
in.docx (19.8 KB)

@alexey.noskov
Thanks for reply, your code is working fine. is it dose not working using our code.
Our sample code have attached.
WorkOrder127.pdf (50.3 KB)

Test181Forum.zip (3.4 KB)

@akondewar It looks like the problem occurs when de-AT culture is set.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-26584

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@akondewar We have completed analyzing the issue and concluded to close it as not a bug. The nested formula field expression has an OR function with arguments separated by comma (,). However, the de-AT culture defines semicolon (;) as a list separator, which is used by MS Word and Aspose.Words to parse function arguments.

You should modify template document (replace , with ;) to get the expected result with de-AT culture.

The issues you have found earlier (filed as WORDSNET-26584) have been fixed in this Aspose.Words for Java 24.3 update.

Hello @alexey.noskov
We would like to skip the entire table if CompPrint = false or isOperationPrint = false.

We have added { SKIPIF { MERGEFIELD CompPrint } = "false" } in table after table start tag, but SKIPIF is not working.

I have attached the sample code, word template and input xml.

ToForum.zip (134.5 KB)

@akondewar SKIPIF field is intended to skip the record in the data source upon executing mail merge. In your case however, you fill SKIPIF field after filling the document with data, so there is nothing to skip at this stage. If it is required to hide a whole table, you should wrap it into the IF field.

@alexey.noskov
following condition is not working while compare ‘isExclude’, any thing I missed ?

{ SKIPIF { = OR ({ COMPARE { MERGEFIELD isExclude } = "8-All Line Items Received" }, { COMPARE { MERGEFIELD closedshort } = true }) } = 1 }

@akondewar Could you please provide a simple template and data you use to fill it with data along with your code that will allow to reproduce the problem? We will check the issue and provide you more information.

On your side you can debug your condition by putting { MERGEFIELD isExclude } = "8-All Line Items Received" } field outside the SKIPIF field and check what value it returns. This will allow you to understand what is wrong with your condition.

Hello @alexey.noskov , thanks now its working for me.

Can we used 3 condition using compare

{ SKIPIF { = OR ({ COMPARE { MERGEFIELD isExclude } = "8-All Line Items Received" }; { COMPARE { MERGEFIELD closedshort } = true }; { COMPARE { MERGEFIELD isExclude } = "7-Items Received" }) } = 1 }

@akondewar Sure, you can use as many condition as it is required for your scenario. But OR accepts only 2 parameters and returns the value 1 (true) if either or both logical expressions x and y are true, or the value 0 (zero) (false) if both expressions are false. See FORMULA field for more information.
So to have three condition you should either use nested OR, something like this { = OR(x, OR(y,z))} or check sum of all conditions. For example:
{ SKIPIF { = { COMPARE { MERGEFIELD test1 } = true } + { COMPARE { MERGEFIELD test2 } = true } + { COMPARE { MERGEFIELD test2 } = true } } > 0 }

@alexey.noskov
I have another following question is

We have 5 lines data which have

If ‘dataExclude=true’, then skipped lines will have isExclude “8-All Line Items Received” OR isExclude = “7-Line Item Received” value.

if Suppressclosedshort = “true” then skipped those lines have closedshort ‘true’ value .

Possibilities are

  1. dataExclude=true and Suppressclosedshort = false

  2. dataExclude=false and Suppressclosedshort = true

  3. dataExclude=true and Suppressclosedshort = true

  4. dataExclude=false and Suppressclosedshort = false

Input XML like
Data like this

<PODetails>
	<isExclude>5-Vendor Notified</isExclude>
	<Product_Description/>
	<Product>PO-00032-1</Product>
	<Line_No>1.000000</Line_No>
	<closedshort>false</closedshort>
	<Unit_Price>10.000000</Unit_Price>
	<Product_Quantity>1.000000</Product_Quantity>
</PODetails>
<PODetails>
	<isExclude>8-All Line Items Received</isExclude>
	<Product_Description/>
	<Product>PO-00032-2</Product>
	<Line_No>2.000000</Line_No>
	<closedshort>false</closedshort>
	<Unit_Price>5.000000</Unit_Price>
	<Product_Quantity>0.000000</Product_Quantity>
</PODetails>
<PODetails>
	<isExclude>8-All Line Items Received</isExclude>
	<Product_Description/>
	<Product>PO-00032-3</Product>
	<Line_No>3.000000</Line_No>
	<closedshort>false</closedshort>
	<Unit_Price>6.000000</Unit_Price>
	<Product_Quantity>5.000000</Product_Quantity>
</PODetails>
<PODetails>
	<isExclude>7-Items Received</isExclude>
	<Product_Description/>
	<Product>PO-00032-4</Product>
	<Line_No>3.000000</Line_No>
	<closedshort>false</closedshort>
	<Unit_Price>6.000000</Unit_Price>
	<Product_Quantity>5.000000</Product_Quantity>
</PODetails>

I tried following condition but it’s not working

{ SKIPIF { = OR  
    { = AND  
        { = COMPARE { MERGEFIELD isExclude } = "8-All Line Items Received" },  
        { = COMPARE { MERGEFIELD dataExclude } = "true" }  
    },  
    { = AND  
        { = COMPARE { MERGEFIELD closedshort } = "true" },  
        { = COMPARE { MERGEFIELD Suppressclosedshort } = "true" }  
    }  
} > 0 }

So kindly let us know what will be the condition?

@akondewar Syntax is not quite right in the provided field code. Syntax of OR should OR(x,y) and syntax of AND is similar, i.e. AND(x,y). There is no need of = signe at the beginning of COMPARE field. So your condition should look like this:

{ SKIPIF { = OR(
    { = AND( 
        { COMPARE { MERGEFIELD isExclude } = "8-All Line Items Received" },  
        { COMPARE { MERGEFIELD dataExclude } = "true" })  
    },  
    { = AND(  
        { COMPARE { MERGEFIELD closedshort } = "true" },  
        { COMPARE { MERGEFIELD Suppressclosedshort } = "true" })  
    }  
} > 0 }

You can always debug your conditions by moving it’s parts outside the SKIPIF and see whether they are evaluated as you expect.