Message-ID: <982410736.9707.1711699707054.JavaMail.web05$@web05> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_9706_1399046877.1711699707054" ------=_Part_9706_1399046877.1711699707054 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Basic String Datasource

Basic String Datasource

Intro

This is a basic de= mo that inserts data into a template document that has two merge fields.

Enter values into the text boxes below and click the button.  The v= alues will be inserted into the template document by WordTemplate and strea= med back to you for viewing in MSWord.

If you are just learning WordTemplate, this demo is a good place to star= t looking at the code.  The general sequence of handling a WordTemplat= e document is as follows:

  1. Create an instance of WordTemplate
  2. Open the template document
  3. Set one or more document data sources or repeat blocks
  4. Call the WordTempla= te.Process() method to pull in the data
  5. Save the document either to disk, or to a response stream to the client= browser.

Code

=20
        private string recipName;
        private string recipCompany;

        /// <summary>
        /// Build the report with WordTemplate
        /// </summary>
        public void GenerateDocument()
        {
            this.recipCompany =3D "SoftArtisans";
            this.recipName =3D "Jon Smith";
            // Form the array of mergefield names.  The elements in this ar=
ray
            // correspond to the names of the merge fields in the template,
            // and each element's array index should correspond to an eleme=
nt in the
            // value array

            string[] NamesArr =3D { "Name", "Company", =
"DateTime" };

            //Form an array containg the values to be inserted
            object[] ValuesArr =3D { recipName, recipCompany, System.DateTi=
me.Now };

            //Create an instance of WordTemplate
            WordTemplate wt =3D new WordTemplate();

            //Open the template document
            string templatePath =3D @"..\..\WordTemplateFiles\BasicTem=
plate.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(@"..\..\WordOutputFiles\BasicTemplate_output.docx&=
quot;);

        }

=20

 

 Downloads

=20

 Template: BasicTem= plate.docx

 Output:  BasicTemplate_output.docx

=20
------=_Part_9706_1399046877.1711699707054--