Page tree

Versions Compared

Key

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

...

Table

...

of

...

Contents

Table of Contents
maxLevel1

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.

...

Setting

...

up

...

the

...

template

...

file

...

  1. Create

...

  1. a

...

  1. new

...

  1. .docx

...

  1. file.

...

  1. Save

...

  1. it

...

  1. as

...

  1. template.docx

...

  1. .

...

  1. Info

...

  1. In

...

  1. the

...

  1. sample

...

  1. code,

...

  1. the

...

  1. completed

...

  1. template

...

  1. file

...

  1. is

...

  1. located

...

  1. in

...

  1. templates/template.docx

...



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

...

  1. Select

...

  1. Field.
    Image Added 
     

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

  3. In the Field Properties section, type "Variable" in the Field name box. Click OK.
    Image Added 

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

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

 

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

  1. Include the SoftArtisans.OfficeWriter.WordWriter

...

  1.  namespace in

...

  1. the

...

  1. code

...

  1. behind.

...

  1. Code Block
    languagec#
    using SoftArtisans.OfficeWriter.WordWriter;

...

  1. Code Block
    languagevb
     Imports SoftArtisans.OfficeWriter.WordWriter

...



  1. Instantiate the WordTemplate object.

     

    Code Block
    languagec#
    WordTemplate WT = new WordTemplate();

...

  1. Code Block
    languagevb
    Dim WT As WordTemplate = New WordTemplate()

...



  1. Open the template file with WordTemplate.Open

     

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

...

  1. Code Block
    languagevb
    WT.Open(Page.MapPath("templates\\template.docx"))

...

  1.  

     

  2. In this example, we'll

...

  1. pull

...

  1. a

...

  1. text

...

  1. value

...

  1. from

...

  1. a

...

  1. textbox

...

  1. on

...

  1. a

...

  1. web

...

  1. form

...

  1. that

...

  1. a

...

  1. user

...

  1. submitted.

...

  1. Get

...

  1. the

...

  1. data

...

  1. value

...

  1. from

...

  1. the

...

  1. web

...

  1. form's

...

  1. text

...

  1. box

    Code Block
    languagec#
    string value = DataValueBox.Text.Trim();

...

  1. Code Block
    languagevb
    Dim value As String = DataValueBox.Text.Trim()

...



  1. 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" }

...

  1. Code Block
    languagevb
    Dim values() As Object = {value}
    Dim columnNames() As String = {"Variable"}

...



  1. Use WordTemplate.SetDataSource

...

  1. to

...

  1. bind

...

  1. the

...

  1. data

...

  1. to

...

  1. template

...

  1. file.

...

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

...

  1. Code Block
    languagevb
    WT.SetDataSource(values, columnNames)

...

  1. WordTemplate has three methods to bind data: WordTemplate.SetDataSource

...

  1. ,

...

  1. WordTemplate.SetRepeatBlock

...

  1. ,

...

  1. and

...

  1. WordTemplate.SetMailMerge

...

  1. .

...

  1. SetDataSource

...

  1. binds

...

  1. a

...

  1. single

...

  1. row

...

  1. of

...

  1. data

...

  1. to

...

  1. the

...

  1. template,

...

  1. where

...

  1. the

...

  1. merge

...

  1. fields

...

  1. can

...

  1. span

...

  1. the

...

  1. entire

...

  1. document.

...

  1. To

...

  1. bind

...

  1. a

...

  1. single

...

  1. value

...

  1. or

...

  1. group

...

  1. of

...

  1. single

...

  1. values,

...

  1. you

...

  1. need

...

  1. to

...

  1. put

...

  1. those

...

  1. in

...

  1. a

...

  1. data

...

  1. set,

...

  1. such

...

  1. as

...

  1. an

...

  1. array

...

  1. and

...

  1. then

...

  1. bind

...

  1. that

...

  1. array

...

  1. to

...

  1. the

...

  1. template

...

  1. file.

...



  1. Call WordTemplate.Process()

...

  1. to

...

  1. bind

...

  1. the

...

  1. data

...

  1. to

...

  1. the

...

  1. template

...

  1. file.

...

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

...

  1. Code Block
    languagevb
    WT.Process()

...



  1. Save the output with WordTemplate.Save

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

...

  1. Code Block
    languagevb
    WT.Save(Response, "Output.docx", False)

...

  1.  

    There are several options for WordTemplate.Save

...

  1. including:

...

  1. save

...

  1. to

...

  1. disk,

...

  1. save

...

  1. to

...

  1. memory

...

  1. stream,

...

  1. stream

...

  1. back

...

  1. to

...

  1. the

...

  1. client

...

  1. inline,

...

  1. and

...

  1. stream

...

  1. back

...

  1. to

...

  1. the

...

  1. client

...

  1. as

...

  1. an

...

  1. attachment.

...

  1. In

...

  1. this

...

  1. case,

...

  1. we're

...

  1. streaming

...

  1. the

...

  1. document

...

  1. back

...

  1. to

...

  1. the

...

  1. client

...

  1. as

...

  1. an

...

  1. attachment.

...

  1. Info

...

  1. 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.

...

  1.  

     

  2. Run your code.

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

    Image Added

 

Congratulations, you completed Hello World using WordTemplate!

Final Code

Section
Column
width50
Code Block
languagec#
 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
Column
width50
Code Block
languagevb
 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

Downloads

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

...