[]
The Process or BatchProcess method of DataTemplate class can be used to process templates in GcWord and generate Word documents with advanced layouts.
The Process method processes the template layout by replacing the template tags with actual data from the datasource. Whereas, the BatchProcess method processes the template layout by iterating over the root items of a data source in a loop to generate separate documents for each data source item. For example, generating multiple offer letters for different candidates or airline tickets for different travellers by using the same template layout.
The Process method can be used to process the template layout and save the data to a single Word document. It replaces the template tags in a document with actual data from data source and generates a final Word Document with desired data.
This following example uses Process method to generate a Word document where a House Rental Agreement containing the information of landlord, tenant and rental charges needs to be created. The static content in the agreement is generic and can be used as it is, time and again. The dynamic fields are specified as placeholders and can be updated every time a new agreement is to be signed. The template layout is created in Word and is populated with actual data after binding with data source which, in this case, is an XML file.
The below steps describe how to generate a House Rental Agreement using a template in Word. You can also download the Template layout here.
RentalAgreement[Template].docx
Create a template layout in a Word document and define the following fields:
Static Fields: Define the static fields in template layout, that is, the fields whose values will remain constant in the final document. For example header fields and information labels like 'Landlord Information', 'Rental Information', 'Phone Number', 'Email', 'Rent Amount per Month' etc.
Bound Fields: Specify the datasource bound fields in mustache braces {{ }} like {{landlordName}}, {{collectedBy}}, {{tenantEmail}} etc.
Load the template created in above step, in GcWord.
GcWordDocument doc = new GcWordDocument();
//Load template layout
doc.Load("RentalAgreement[Template].docx");
Initialize a DataSet as data source and read data from an XML file. After adding the XML as dataset, add one of the tables as datasource for the template.
//Initialize DataSet
var ds = new System.Data.DataSet();
//Read data from xml
ds.ReadXml(Path.Combine("GcWordTplDataSet.xml"));
//Add datasource
doc.DataTemplate.DataSources.Add("ds", ds.Tables["HouseRentalAgreement"]);
The data source used in this sample (GcWordTplDataSet.xml) can also be downloaded from Resources/data folder of this project.
Process the template by using Process method.
//Process the template
doc.DataTemplate.Process();
Save the final Word document.
//Save the final document
doc.Save("RentalAgreement.docx");
The output of the House Rental Agreement is shown as below:
The BatchProcess method can be used to process the template layout and save multiple rows of data into separate Word documents.
The following example saves data of different employees to separate Word documents. The template layout is created in Word and is populated with actual data after binding with data source. You can also download the Template layout here.
Create a template layout in a Word document and define the following fields:
Static Fields: Define the static fields in template layout, that is, the fields whose values will remain constant in the final document. For example Name, Designation, Years of Experience etc.
Bound Fields: Specify the datasource bound fields in mustache braces {{ }} like {{name}}, {{designation}}, {{experience}} etc.
Load the template created in above step, in GcWord.
GcWordDocument doc = new GcWordDocument();
//Load template layout
doc.Load("EmployeeData[Template].docx");
Add data which will be used as data source by using DataSources method of DataTemplate class.
doc.DataTemplate.DataSources["ds"] = new object[] {
new {
name = "Derek Clark",
designation = "HOD",
department = "marketing",
experience = "23"
},
new {
name = "Jessica Adams",
designation = "Senior Executive",
department = "sales",
experience = "5"
},
new {
name = "Anil Mittal",
designation = "Software Engineer",
department = "development",
experience = "7"
}
};
Process the template by using BatchProcess method and save the Word documents separately.
// produces a document for each root item in the data source collection
int i = 0;
doc.DataTemplate.BatchProcess(() =>
{
doc.Save($"Employee-Data-{++i}.docx");
});
The output of Employee Report Templates is saved to separate documents as shown below:
Limitation
The template layout document with multiple sections is not batch processed correctly. Hence, in case of batch processing, keep the template layout in a single section only.