Document Solutions for Word
Report Templates / Walkthrough / Generate Document with Advanced Layout
In This Topic
    Generate Document with Advanced Layout
    In This Topic

    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.

    Create a Word Document using Process method

    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.

    1. 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.        


    2. Load the template created in above step, in DsWord.
      C#
      Copy Code
      GcWordDocument doc = new GcWordDocument();
      //Load template layout
      doc.Load("RentalAgreement[Template].docx");

    3. 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.
      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"]);
      The data source used in this sample (DsWordTplDataSet.xml) can also be downloaded from Resources/data folder of this project.

    4. Process the template by using Process method.
      C#
      Copy Code
      //Process the template
      doc.DataTemplate.Process();

    5. Save the final Word document.
      C#
      Copy Code
      //Save the final document
      doc.Save("RentalAgreement.docx");

      The output of the House Rental Agreement is shown as below:

    Create Separate Word Documents using BatchProcess Method

    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.

    1. 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.        

              
    2. Load the template created in above step, in DsWord.
      C#
      Copy Code
      GcWordDocument doc = new GcWordDocument();
      //Load template layout
      doc.Load("EmployeeData[Template].docx");

    3. Add data which will be used as data source by using DataSources method of DataTemplate class.
      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"
                }
            };


    4. Process the template by using BatchProcess method and save the Word documents separately.
      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");
      });

         

      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.