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.