Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
         private string recipName;
        private string recipCompany;
 
        /// <summary>
        /// Build the report with WordTemplate
        /// </summary>
        public void GenerateDocument()
        {
            this.recipCompany = "SoftArtisans";
            this.recipName = "Jon Smith";
            // Form the array of mergefield names.  The elements in this array
            // correspond to the names of the merge fields in the template,
            // and each element's array index should correspond to an element in the
            // value array
           
 
            string[] NamesArr = { "Name", "Company", "DateTime" };
 
            // Form an array containg the values to be inserted
            object[] ValuesArr = { recipName, recipCompany, System.DateTime.Now };
 
            // Create an instance of WordTemplate
            WordTemplate wt = new WordTemplate();
 
            // Open the template document
            string templatePath = @"..\..\templates\BasicTemplate.docx";
            wt.Open(templatePath);
 
            // Set the main data source with the Name and Value arrays
            wt.SetDataSource(ValuesArr, NamesArr);
 
            // Populate the template to pull in the new values
            wt.Process();
 
            //Save the populated document
            wt.Save(@"..\..\output\BasicTemplate_output.docx");
 
        }
    }
}

\

 Downloads

Panel

 Template: BasicTemplate.docx

 Output:  BasicTemplate_output.docx