Versions Compared

Key

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

...

  1. Create a new .docx file. Save it as template.docx.

    Info

    In the sample code, the completed template file is located in templates/template.docx



  2. Go to the Insert tab on the ribbon > Text group > Quick Parts drop-down. Select Field.
     
     

  3. In the Field dialog, select Mergefield from the Field names list.
     

  4. In the Field Properties section, type "Variable" in the Field name box. Click OK.
     

    This will create a merge field <<Variable>> which will correspond to a column in a data set with only one row of data.
     
     

  5. The template is now complete. We'll move on to writing the code to bind the data to this merge field.
     

...

  1. Include the SoftArtisans.OfficeWriter.WordWriter namespace in the code behind.

    Code Block
    languagec#
    using SoftArtisans.OfficeWriter.WordWriter;
    Code Block
    languagevb
     Imports SoftArtisans.OfficeWriter.WordWriter



  2. Instantiate the WordTemplate object.

     

    Code Block
    languagec#
    WordTemplate WT = new WordTemplate();
    Code Block
    languagevb
    Dim WT As WordTemplate = New WordTemplate()



  3. Open the template file with WordTemplate.Open

     

    Code Block
    languagec#
    WT.Open(Page.MapPath("templates\\template.docx"));
    Code Block
    languagevb
    WT.Open(Page.MapPath("templates\\template.docx"))

     

     

  4. In this example, we'll pull a text value from a textbox on a web form that a user submitted. Get the data value from the web form's text box

    Code Block
    languagec#
    string value = DataValueBox.Text.Trim();
    Code Block
    languagevb
    Dim value As String = DataValueBox.Text.Trim()



  5. Create an object array to hold the textbox value. Create a string array to hold the column name "Variable"

    Code Block
    languagec#
    object[] values = { value };
    string[] columnNames = { "Variable" }
    Code Block
    languagevb
    Dim values() As Object = {value}
    Dim columnNames() As String = {"Variable"}



  6. Use WordTemplate.SetDataSource to bind the data to template file.

    Code Block
    languagec#
    WT.SetDataSource(values, columnNames);
    Code Block
    languagevb
    WT.SetDataSource(values, columnNames)

    WordTemplate has three methods to bind data: WordTemplate.SetDataSource, WordTemplate.SetRepeatBlock, and WordTemplate.SetMailMerge. SetDataSource binds a single row of data to the template, where the merge fields can span the entire document. To bind a single value or group of single values, you need to put those in a data set, such as an array and then bind that array to the template file.

     



  7. Call WordTemplate.Process() to bind the data to the template file.

    Code Block
    languagec#
    WT.Process();
    Code Block
    languagevb
    WT.Process()



  8. Save the output with WordTemplate.Save

    Code Block
    languagec#
    WT.Save(Response, "Output.docx", false);
    Code Block
    languagevb
    WT.Save(Response, "Output.docx", False)

     

    There are several options for WordTemplate.Save including: save to disk, save to memory stream, stream back to the client inline, and stream back to the client as an attachment. In this case, we're streaming the document back to the client as an attachment.

    Info
    WordWriter does not convert between file formats, so it is important that the file extension on the output file matches the file extension of the original template file.

     

     

  9. Run your code.

    In the output file you will see that the merge field has been replaced by the value.

...