Page tree

Versions Compared

Key

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

Intro

Excerpt

A fax cover sheet with recipient and sender information, created by WordTemplate.


The template document is a formatted fax cover form with merge fields for every string, bool or int in the code. 

Code

Code Block

  public class FaxCoverGenerator
    {
 
        private string toName, fromName, company, faxNumber, phoneNumber, subject, comments;
        private int pageCount;
        private bool isUrgent, isForReview, isCommentRequested;
 
        /// <summary>
        /// Build the report with WordTemplate
        /// </summary>
        public void GenerateDocument()
        {
            //Desired values for the cover sheet:
            this.toName = "Jon Smith";
            this.fromName = "Robert Jones";
            this.company = "SoftArtisans";
            this.faxNumber = "(123)-456-7890";
            this.phoneNumber = "(123)-555-5555";
            this.subject = "Sales Report";
            this.comments = "Attached is the sales report.";
            this.isUrgent = true;
            this.isForReview = true;
            this.isCommentRequested = true;
            this.pageCount = 10;
 
            // 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 correpond to an element in the value array
           
            string[] arrNames = {"ToName", "FromName", "Company", "FaxNumber", "PhoneNumber",
                                    "Subject", "PageCount", "Comments", "ChkUrgent", "ChkReview", "ChkComment"};
 
            // 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 = {this.toName, this.fromName, this.company, this.faxNumber, this.phoneNumber,
                                     this.subject, this.pageCount, this.comments,
                                     this.isUrgent?"X":"", this.isForReview?"X":"", this.isCommentRequested?"X":""};
 
 
            // Create the WordTemplate object
            WordTemplate wt = new WordTemplate();
 
            // Open the template document.
            string templatePath = @"..\..\WordTemplateFiles\FaxCoverTemplate.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
            wt.Save(@"..\..\WordOutputFiles\FaxCover_output.docx");
 
        }
 

 Downloads

Panel

Template:  FaxCoverTemplate.docx

Output:  FaxCover_output.docx