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

Free Support Forum - aspose.com

Doc generation using Custom json data Source with specific filed from template

Hi Team,

We are trying to implement the custom json datasource using IMailMergeDataSource.

Json:-

{
 "pb_lastname_1": null,
 "pb_lastname_2": null,
 "receivedmethod": "E-mail",
 "pb_lastname_3": null,
 "success": true,
 "auditstatusaction": null,
 "ordernumber_old": null,
 "parties": [
 {
 "partyseq": 1,
 "userpartytype": "PB",
 "firstname": "Lee",
 "middlename": null,
 "lastname": "Salzmann",
 "usersuffix": null,
 "email": "lee@test.com",
 "companyname": null,
 "address1": "28 Winoke Drive",
 "address2": null,
 "city": "Huntington Station",
 "state": "NY",
 "zip": "11746",
 "phone": null,
 "fax": null
 },
 {
 "partyseq": 2,
 "userpartytype": "PB",
 "firstname": "Mike",
 "middlename": null,
 "lastname": "S",
 "usersuffix": null,
 "email": "mike@test.com",
 "companyname": null,
 "address1": "28 Winoke Drive",
 "address2": null,
 "city": "Huntington Station",
 "state": "NY",
 "zip": "11746",
 "phone": null,
 "fax": null
 }
 ]
 }

How can we implement custom jsondatasource which will generate the data based on specific filed we will pass in tempalte. either it could be parents data or child data.
like

«pb_lastname_1» «pb_lastname_2» or «parties[0]. userpartytype» without using executeWithRegions or fill a table. and what expression we have to use in word template for specific child filed.

Please help.

Thanks in Advance .
Regards,
Dilip

Hi Dilip,

Thanks for your inquiry. Yes, you can implement IMailMergeDataSource interface to achieve your requirements. Please follow the steps mentioned below to achieve your requirements.

  1. Create a new class e.g class1 which implement IMailMergeDataSource.
  2. Convert object of this class to JSON using Gson
  3. Use JSON as data source for mail merge

Please download Aspose Cloud SDK for Java from here and check the following code example for your kind reference. I have attached the related classes and template document with this post. You can use the same approach to achieve your requirements. Hope this helps you.

//Create some data that we will use in the mail merge.
CustomerList customers = new CustomerList();
customers.add(new Customer("Thomas Hardy", "120 Hanover Sq., London"));
customers.add(new Customer("Paolo Accorti", "Via Monte Bianco 34, Torino"));
CustomerMailMergeDataSource customersDataSource = new CustomerMailMergeDataSource(customers);
Gson gson = new Gson();
String strJSON = gson.toJson(customersDataSource, CustomerMailMergeDataSource. class);
MailMerge mm = new MailMerge();
mm.executeMailMerege("CustomDataSource.doc", strJSON, SaveFormat.Docx, "c:/temp/Out.docx");