Aspose word single template merge fields render multiple time using java not for table

aspose word single template merge fields render multiple time using java not for table

@kalpeshAspose1997 What you are asking about is called Mail Merge with Regions. Please see our documentation to learn more:

Explain you in brief I have created one template that contains 2 pages, now suppose the data of value contains 1 value then it will fill up template 2 pages and if the data contains 3 value of field then it should be fill up template with 2 (page) * 3 (data) = 6 page of template, let us know if you still need to know anything

@kalpeshAspose1997 Could you please create a sample template and provide sample data and expected output? Unfortunately, it is not quite clear what you are trying to achieve.

if testCaseData contain twice in a json data then whole templet should be execute twice in same docx, let us know if any doubt

@kalpeshAspose1997 Thank you for additional information. It looks like nested mail merge with regions is what you need. I have create a simple code example that demonstrates the technique (DataSet is used as a data source for demonstration, but you can use implementation of IMailMergeDataSource and the appropriate getChildDataSource method to return data source for the child region.)
in.docx (13.9 KB)
out.docx (11.5 KB)

// Define data set and data tables
DataSet ds = new DataSet();
DataTable root = new DataTable("root");
DataTable child = new DataTable("child");

// Add tables into the data set
// Define relation between the tables.
ds.getRelations().add(root.getColumns().get("id"), child.getColumns().get("root_id"));

// Put some dummy data into the tables.
// In your case data will be read from JSON.
root.getRows().add(1, "First Group");
root.getRows().add(2, "Second Group");
root.getRows().add(3, "Third Group");
child.getRows().add(1, "First item in the First Group");
child.getRows().add(1, "Second item in the First Group");
child.getRows().add(1, "Third item in the First Group");
child.getRows().add(2, "First item in the Second Group");
child.getRows().add(2, "Second item in the Second Group");
child.getRows().add(3, "Lonely item in the Third Group");

// Open template and execute mail merge with regions.
Document doc = new Document("C:\\Temp\\in.docx");

In your case, the root region (testCaseData) octuply whole template. So when there are more than one testCaseData in your datasource, the whole region is repeated, just like in the simple example I have provided.

is there any way to achieve this without use table ? if yes can you send me details for same

@kalpeshAspose1997 Dou you mean without using DataSet and DataTable classes? Sure, you can implement IMailMergeDataSource.

Can we can add If condition in template if yes then how can set dynamic value for that

@kalpeshAspose1997 Sure, you can use standard MS Word IF fields. As a dynamic values you can use merge field. Field code will look like this:
{ IF "{ MERGEFIELD test }" = "true" "True value" "False Value" }

You should use Ctrl+F9 to type field code in MS Word.

Also, Aspose.Words offers more advanced LINQ Reporting Engine. You can consider using it instead of standard Mail Merge.

thanks, I want to add mail merge field in the template with if condition for table enable/disable dynamically, for that can you suggest best approach?

@kalpeshAspose1997 You can put your table into True/False values of IF field. For example see the following simple template and code:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().execute(new String[] {"test"}, new Object[] {"1"});"C:\\Temp\\out.docx");

in.docx (13.0 KB)
out.docx (10.5 KB)

thanks my friend for support

1 Like

for nested Table If data is not present it will showing merge fields as it is so is there way to replace specific character if data is not present if yes can you suggest me approach for same?
thank you

@kalpeshAspose1997 You can use mail merge cleanup options to remove unused fields from the document after executing mail merge. Please see our documentation for more information:

after all data tables and dataset set in code, want to update list of merge fields with list of values so can you send me example for that

@kalpeshAspose1997 Unfortunately, it is not quite clear what you need to achieve. If you need to get a list of field in your template, you can use MailMerge.getFieldNames() method.

I want to get all merge fields and update with that all fields with values so for that is there any way to achieve that?

public void execute(String[] fieldNames, Object[] values) 

method like this, I want use executeWithRegions method is available to achieve this?

@kalpeshAspose1997 Thank you for additional information. MailMarge.execute and MailMarge.executeWithRegions method do exactly this - updated the fields in the template with actual values from the data source used to execute mail merge.

1 Like

@kalpeshAspose1997 Could you please elaborate what exactly does not work as expected and what is the expected behavior?
Also, I see you use the same merge field in two conditions and check whether it’s value is true/false. You can achieve the same using one condition:
{ IF "{ MERGEFIELD stc }" = "true" "True Content" "False Content"}

In addition put your merge field and the value to compare with in the condition into double quotes and there should be whitespace before and after condition operator.

1 Like