Page tree

Versions Compared

Key

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

...

Code Block
   public class FormLetterGenerator
    {
 
        private string recipName, recipStreetAddr, recipCity,
            recipState, recipZip, authName, authTitle;
        /// <summary>
        /// Build the report with WordTemplate
        /// </summary>
        public void GenerateDocument()
        {
            //Information:
            this.recipName = "Jon Smith";
            this.recipStreetAddr = "123 Main Street";
            this.recipCity = "Springfield";
            this.recipState = "CO";
            this.recipZip = "05555";
            this.authName = "Sarah White";
            this.authTitle = "CEO";
          
            // Form the required WordWriter name array.  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[] arrNames = {"RecipientName", "RecipientStreetAddr", "RecipientCity",
                                    "RecipientState", "RecipientZip", "AuthorName", "AuthorTitle"};
 
            // The value array.  The value of the elements in this array
            // contain info supplied in the web form. These values
            // represent the data that will be dynamically populated
            // in the template file.
           
            object[] arrValues = {recipName, recipStreetAddr, recipCity,
                                     recipState, recipZip, authName, authTitle};
 
 
            // Create the WordTemplate object
            WordTemplate wt = new WordTemplate();
 
            // Open the template document
            string templatePath = @"..\..\WordTemplateFiles\FormLetterTemplate.docx";
            wt.Open(templatePath);
 
            // Set the datasource with the name and value arrays defined above
            wt.SetDataSource(arrValues, arrNames);
 
            // Process the template
            wt.Process();
            //Save the document in the desired location
            wt.Save(@"..\..\WordOutputFiles\FormLetter_output.docx");
        }
 
    }
 
 

 Downloads

Panel

 TemplateTemplate:  FormLetterTemplate.docx

Output:  FormLetter_output.docx

...