Hello,
I try to find an example where a table is create without connexion to date source and word template but I found anything in the documentation.
Actually I have a function that create a PDF document with MailMerge. All fields are String. and use the excute and save method.
I would like to know how to declare in the template word and how to use the MailMerge for create a table without connexion of the dataSource. The table content is saved in a java list.
Thank you for your help.
Hi Sophie,
Thanks for your interest in Aspose.Words. What I understand, you need to perform mail merge operation using DataTable class. Please try running the following code and see the attached input/output Word documents:
Document doc = new Document("C:\Temp\template.docx");
DataTable dataTable = new DataTable();
dataTable.getColumns().add("Name");
dataTable.getColumns().add("AGE");
DataRow row = dataTable.newRow();
row.set(0, "James");
row.set(1, "25");
dataTable.getRows().add(row);
row = dataTable.newRow();
row.set(0, "XYZ");
row.set(1, "30");
dataTable.getRows().add(row);
row = dataTable.newRow();
row.set(0, "ABC");
row.set(1, "25");
dataTable.getRows().add(row);
doc.getMailMerge().execute(dataTable);
doc.save("C:\Temp\out.docx");
Please also read the following articles to learn about the mail merge functionality of Aspose.Words:
https://docs.aspose.com/words/java/mail-merge-template/
https://docs.aspose.com/words/java/types-of-mail-merge-operations/
Please let me know if I can be of any further assistance.
Best regards,
Hello,
Thank you for the answer but is not exactly what I want. I do not want the template is repate for each element of the list. I want a word table. The idea is to have the same thing of the page : https://docs.aspose.com/words/java/how-to-build-a-table-from-a-datatable/
but with using Mail Merge and not DocumentBuilder and no datasource.
In a attached file :
- template.doc : begin of my template
- out.pdf : The wanted result
- java_code.txt : the code does not compile, it is just for you can understand my configuration (it is some extract of my test code)
Hi Sophie,
Thanks for the additional information. To achieve this, you need to use mail merge with regions functionality of Aspose.Words. I have modified your template document and attached it here for your reference; please try running the following code:
Document doc = new Document("C:\\Temp\\template.doc");
DataTable dataTable = new DataTable("r1");
dataTable.getColumns().add("col1");
dataTable.getColumns().add("col2");
DataRow row;
for (int i = 0; i < 5; i++)
{
row = dataTable.newRow();
row.set(0, "Line " + i);
row.set(1, "Line " + i);
dataTable.getRows().add(row);
}
doc.getMailMerge().executeWithRegions(dataTable);
doc.save("C:\\Temp\\out.pdf");
I hope, this helps.
Best regards,
Hello,
Thank you for your help. I try to do this way yesterday but I haven’t find the constructor or setter for the NameTable attribute but with the version 13.9.0 of today it’s work
Thank you and have a nice day.
Hi Sophie,
Thanks for your feedback. It’s great you were able to find what you were looking for. Please let us know any time you have any further queries.
Best regards,