|| Table of Contents ||
| {toc} |

h1. _Hello World_ with WordTemplate

WordWriter's WordTemplate approach allows you to write data to a template file that contains merge field. The merge fields tell WordWriter where to bind specific sets of data, similar to Word's mail merge. This tutorial will show you the basics on how to dynamically insert data into a document using WordTemplate by taking custom text from a web form textbox and inserting it into a template file.

h1. Setting up the template file

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{info}

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


!Step2.png|border=1,width=700!


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.

!Step4.png|border=1!

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.

!StepFinal.png|border=1,width=700!

h1. Writing the code

{info}In the sample code, the sample web application page WorldTemplate_HelloWorld.aspx and code behind WordTemplate_HelloWorld.aspx.cs/vb are available in the Hello World project. {info}

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

{csharp:1}
using SoftArtisans.OfficeWriter.WordWriter;
{csharp}

{vbnet:1}
Imports SoftArtisans.OfficeWriter.WordWriter
{vbnet}

2. Instantiate the [WordTemplate] object.

{csharp:2}
WordTemplate WT = new WordTemplate();
{csharp}

{vbnet:2}
Dim WT As WordTemplate = New WordTemplate()
{vbnet}

3. Open the template file with [WordTemplate.Open]

{csharp:3}
WT.Open(Page.MapPath("templates\\template.docx"));
{csharp}

{vbnet:3}
WT.Open(Page.MapPath("templates\\template.docx"))
{vbnet}

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

{csharp:4}
string value = DataValueBox.Text.Trim();
{csharp}

{vbnet:4}
Dim value As String = DataValueBox.Text.Trim()
{vbnet}

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

{csharp:5}
object[] values = { value };
string[] columnNames = { "Variable" }
{csharp}

{vbnet:5}
Dim values() As Object = {value}
Dim columnNames() As String = {"Variable"}
{vbnet}

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

{csharp:6}
WT.SetDataSource(values, columnNames);
{csharp}

{vbnet:6}
WT.SetDataSource(values, columnNames)
{vbnet}

WordTemplate has three methods to bind data: [SetDataSource|WordTemplate.SetDataSource], [SetRepeatBlock|WordTemplate.SetRepeatBlock], and [SetMailMerge|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.

{csharp:7}
WT.Process();
{csharp}

{vbnet:7}
WT.Process()
{vbnet}

8. Save the output with [WordTemplate.Save]
{csharp:7}
WT.Save(Response, "Output.docx", false);
{csharp}

{vbnet:7}
WT.Save(Response, "Output.docx", False)
{vbnet}

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.{info}

9. Run your code.

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

!Output.png|border=1,width=700!

Congratulations, you completed _Hello World using WordTemplate_\!

h1. Final Code

{csharp:8}
using SoftArtisans.OfficeWriter.WordWriter;
...
WordTemplate WT = new WordTemplate();

WT.Open(Page.MapPath("templates\\template.docx"));

string value = DataValueBox.Text.Trim();

object[] values = { value };
string[] columnNames = { "Variable" };

WT.SetDataSource(values, columnNames);

WT.Process();

WT.Save(Response, "Output.docx", false);
{csharp}

{vbnet:8}
Imports SoftArtisans.OfficeWriter.WordWriter
...
Dim WT As WordTemplate = New WordTemplate()

WT.Open(Page.MapPath("templates\\template.docx"))

Dim value As String = DataValueBox.Text.Trim()

Dim values() As Object = {value}
Dim columnNames() As String = {"Variable"}

WT.SetDataSource(values, columnNames)

WT.Process()

WT.Save(Response, "Output.docx", False)
{vbnet}

h1. Downloads

You can download the code for the Hello World tutorial as a Visual Studio solution.

* [Hello World Tutorial^WordWriter_HelloWorldC#.zip]
* [Hello World Tutorial^WordWriter_HelloWorldVB.zip]