The Process or BatchProcess method of DataTemplate class can be used to process templates in DsWord 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.
C# |
Copy Code |
---|---|
GcWordDocument doc = new GcWordDocument(); //Load template layout doc.Load("RentalAgreement[Template].docx"); |
C# |
Copy Code |
---|---|
//Initialize DataSet var ds = new System.Data.DataSet(); //Read data from xml ds.ReadXml(Path.Combine("DsWordTplDataSet.xml")); //Add datasource doc.DataTemplate.DataSources.Add("ds", ds.Tables["HouseRentalAgreement"]); |
C# |
Copy Code |
---|---|
//Process the template
doc.DataTemplate.Process(); |
C# |
Copy Code |
---|---|
//Save the final document doc.Save("RentalAgreement.docx"); |
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.
C# |
Copy Code |
---|---|
GcWordDocument doc = new GcWordDocument(); //Load template layout doc.Load("EmployeeData[Template].docx"); |
C# |
Copy Code |
---|---|
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" } }; |
C# |
Copy Code |
---|---|
// produces a document for each root item in the data source collection int i = 0; doc.DataTemplate.BatchProcess(() => { doc.Save($"Employee-Data-{++i}.docx"); }); |
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.