How to use regions in mail merge/smart marker in ms word template?

Doubt.docx (14.5 KB)

Could you please find the attachment for my requirement/doubt.

  1. How do we create the template file in ms word with these smart markers OR mail merge fields manually?
  2. Can you provide java code snippet to achieve this?

NOTE: Template file needs to have all the above smart markers/ mail merge fields.

@Rakesh_M

  1. Please see our documentation to learn how to create mail merge template:
    https://docs.aspose.com/words/java/mail-merge-template/
    In your document example you use mustache syntax. Please see the following article for more information:
    https://docs.aspose.com/words/java/mail-merge-template-from-mustache-syntax/

  2. If you need to insert merge fields programmatically, you can use DocumentBuilder.insertField method. See our documentation for more information:
    https://docs.aspose.com/words/java/insert-fields/

Hi ,
I just tried the existing (Mail Merge Template from Mustache Syntax|Aspose.Words for Java )example, but im not getting out put with the data populated.
Please find the attachment for my files, template file and output file.

RealTemplate.docx (13.3 KB)
wordTopdfList.pdf (45.9 KB)

How do we create the template with smart marker manually.

@Rakesh_M Your template works fine on my side. I have used the following simple code for testing:

DataTable dt = new DataTable("list");
dt.getColumns().add("Number");
for (int i = 0; i < 10; i++)
    dt.getRows().add(i);

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setUseNonMergeFields(true);
doc.getMailMerge().executeWithRegions(dt);
doc.save("C:\\Temp\\out.pdf");

out.pdf (22.5 KB)

yes, now working.Thanks.
I have 1 more query. Please find the attachment.

Is this template is correct?
1.I want to repeat the name of the customer.
2. I want to repeat the department and professorsDoubt.docx (14.6 KB)
.

@Rakesh_M Template syntax depends on your data source. It looks like you need nested mail merge feature. Please see our documentation for more information:
https://docs.aspose.com/words/java/nested-mail-merge-with-regions/

Ok, Thanks,i’ll look into that.
1Query:
i’m executing the example application
( https://reference.aspose.com/words/java/com.aspose.words/imailmergedatasource/ )
this is forking fine with

doc.getMailMerge().execute(customersDataSource);

but if I change to executeWithRegions it is not working. What can i do to make it work?

doc.getMailMerge().executeWithRegions(customersDataSource);

@Rakesh_M Make sure in your implementation of IMailMergeDataSource, the method getTableName returns correct table name. For example if in your implementation getTableName looks like this:

public String getTableName() {
    return "Customer";
}

The region should look like this:
{{ #foreach Customer }}.......{{ /foreach Customer }}

No, It is not working . Please find the attachment for code base and the output.

doubt.zip (61.3 KB)

Could you please provide your suggestions.

adding few issues to existing sample application.

  1. doc.getMailMerge().executeWithRegions(customersDataSource);
    if i run with executeWithRegions() method, it is not displaying the complete output for regions. It is displaying only one value. Please find the the output below specified.

«FullName»
«Address»
• xyz

  1. doc.getMailMerge().executeWithRegions(customersDataSource);
    if i run with execute() method, it is not displaying the
    regions data. nd it is displaying like this
    «TableStart:list»«listofDoc»«TableEnd:list»

Could you please confirm , is this one expected ?
Please find the out put attachments.

executeWithRegions_output.pdf (43.3 KB)
execute_output.pdf (49.3 KB)

@Rakesh_M In your code you are execution only simple mail merge as a result only field outside the regions are filled. To fill both fields outside the region and the fields inside region, you should execute simple mail merge and mail merge with regions.

How do i do that both?
Can i execute both these doc.getMailMerge().executeWithRegions(customersDataSource);
doc.getMailMerge().execute(customersDataSource);

Could you also please check my query before i asked.

@Rakesh_M Yes, you can execute both execute and executeWithRegions methods. But in your case I think you should use nested mail merge with regions. In this case your template should look like this:

 {{ #foreach Customers }}
      .......
      {{ #foreach list }}.......{{ /foreach list }}
 {{ /foreach Customers }}

In this case root data source is Customers and for each customer record in IMailMergeDataSource the getChildDataSource should return data source for list.

  1. For Customers object , just i’m holding Fullname and Address that’s it.

there is no dependency between customer and listofDoc.

i need to run those Fullname and address are independent smart markers and listofDoc is a separate one.

{{FullName}}

{{Address}}

• {{ #foreach list }}{{listofDoc}}{{ /foreach list }}

listofDoc is need to be repeated ,like i want to use regions for this and Fullname and Address are independent.

Could you please suggest now?

in simple words , i want to run few smart markers with out repeating (no region is required)
and few are required with regions(need to repeat).

how do i execute this?
please advise.

Hi,
I’m attaching my simple application. Please find the attachments.
issue is:
Only region data is displaying.
Issue With executeWithRegions.zip (56.6 KB)

Could you please check my application and please suggest the solution.
I’m attaching the output file as well.

@Rakesh_M In your code only mail merge with regions is executed. As I mentioned earlier to fill both fields outside the region and the fields inside region, you should execute simple mail merge and mail merge with regions.
Simple mail merge to fill fields outside the region.
Mail merge with regions to fill the region with data.

Hi,
Still not working, please find the attachment for out put files.
1.
TemplateToPdf2 .pdf file is output for below specified order.

doc.getMailMerge().executeWithRegions(ds);
doc.getMailMerge().execute(ds);

TemplateToPdf2.pdf (44.1 KB)
TemplateToPdf1.pdf (45.6 KB)
file is output for below specified order.

doc.getMailMerge().execute(ds);
doc.getMailMerge().executeWithRegions(ds);

Could you pls advise for the solution?TemplateToPdf2.pdf (44.1 KB)

@Rakesh_M In your case you should use separate data sources for region and for fields outside the region, since, as you have mentioned, region data and data outside the region are not related. Foer example see the following code:

DataTable dt = new DataTable("list");
dt.getColumns().add("Number");
for (int i = 0; i < 10; i++)
    dt.getRows().add("item" + i);

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setUseNonMergeFields(true);
// Execute simple mail merge to fill fields outside the region.
// For demonstration purposes I use arrays as data source
doc.getMailMerge().execute(new String[] { "cName", "addressLine", "acount" }, new String[] { "James Bond", "MI5", "007" });
// Execute mail merge with regions to fill region with data.
doc.getMailMerge().executeWithRegions(dt);
doc.save("C:\\Temp\\out.pdf");

in.docx (13.6 KB)
out.pdf (25.6 KB)