Intro

Dynamically create a standard business-style #10 envelope with WordTemplate.


This is just as easy as the other WordTemplate demos: Open the template, set data sources, and save.  The key to this demo is the template file.  It is dimensioned in Word to fit the exact dimensions of a #10 envelope.

Keep in mind that there is no limit to what you can put in the Word template document, and no limit to how it can be formatted.  WordTemplate will preserve 100% of the design and objects in the document while replacing the merge fields with your data. 

Code

 private string returnName, returnStreet, returnCityStateZip,
                    recipName, recipStreet, recipCityStateZip;
        /// <summary>
        /// Build the report with WordTemplate
        /// </summary>
        public void GenerateDocument()
        {
            //Set the recip and return adresses to the desired adresses:
            returnName = "Jon Smith";
            returnStreet = "123 Main St";
            returnCityStateZip = "Springfield, WA 05555";
 
            recipName = "Robert Jones";
            recipStreet = "321 Orange Rd.";
            recipCityStateZip = "Greenville, IL 04444";
 
 
            // 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[] arrNames = {"RtnName", "RtnStreet", "RtnCityStateZip",
                                "RcpName", "RcpStreet", "RcpCityStateZip"};
 
            // 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.returnName, this.returnStreet, this.returnCityStateZip,
                                     this.recipName, this.recipStreet, this.recipCityStateZip};
 
            // Instantiate the WordTemplate object
            WordTemplate wt = new WordTemplate();
 
            // Open the template file
            string templatePath = @"..\..\WordTemplateFiles\EnvelopeTemplate.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 to the desired location
            wt.Save(@"..\..\WordOutputFiles\Envelope_output.docx");
        }
 
    }
 

Downloads

Template:  EnvelopeTemplate.docx

Output:  Envelope_output.docx