Message-ID: <1753877732.8419.1711645348651.JavaMail.web05$@web05> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_8418_852734709.1711645348651" ------=_Part_8418_852734709.1711645348651 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Form Letter Formatting

Form Letter Formatting

Intro

Ge= nerate a form letter with WordTemplate, then pass it to WordApplication to = have formatting applied based on the user's selections.

This demo's template document is in the format of a standard form letter= .  The recipient and author information is inserted into the document = dynamically.  You could also write the letter text and image into the = document using WordTemplate.

After the form letter is generated with WordTemplate, the document is pa= ssed to WordApplication, which then applies formatting (font, font size, et= c) based on the user's selections.

Code

=20
// This data will be imported
private string recipName =3D "Bradley Smith";
private string recipStreetAddr =3D "123 College Street";
private string recipCity =3D "Boston";
private string recipState =3D "MA";
private string recipZip =3D "02201-1020";
private string authName =3D "John Doyle";
private string authTitle =3D "Mr.";
// Font specifications that will be applied to the document
private string fontName =3D "Arial";
private double fontSize =3D 10;

/// <summary>
/// Build the report with WordTemplate.  Then pass it to WordApplicati=
on for formatting.
/// </summary>
public void GenerateDocument()
{
 // Form the required WordWriter name array.  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 =3D {"RecipientName", "RecipientStreetAdd=
r", "RecipientCity", 
 "RecipientState", "RecipientZip", "AuthorName&quo=
t;, "AuthorTitle"};

 // 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 =3D {recipName, recipStreetAddr, recipCity, 
  recipState, recipZip, authName, authTitle};


 // Create the WordTemplate object 
 WordTemplate wt =3D new WordTemplate();

 // Open the template document 
 string templatePath =3D @"..\..\WordTemplateFiles\FormLetterFormattin=
gTemplate.doc";
 wt.Open(templatePath);

 // Set the datasource with the name and value arrays defined above 
 wt.SetDataSource(arrValues, arrNames);

 // Process the template 
 wt.Process();

 // Create the WordApplication object 
 WordApplication wwapp =3D new WordApplication();

 // Open the processed WordTemplate document for formatting 
 Document doc =3D wwapp.Open(wt);

 // Format the document with the selected font and font size 
 FormatCharacterRuns(doc);

 // Save the document by streaming it 
  // to the client's browser 
  
 wwapp.Save(doc, @"..\..\WordOutputFiles\FormLetterFormatted_out.doc&q=
uot;);
}

// Formats all CharacterRuns in the specified Element with the specified fo=
nt name and size 
private void FormatCharacterRuns(Element e)
{
 // If this element is a CharacterRun, format it 
 if (e.ElementType =3D=3D Element.Type.CharacterRun)
 {
 CharacterRun run =3D (CharacterRun) e;
 run.Font.FontName =3D this.fontName;
 run.Font.FontSize =3D this.fontSize;
 }

 // For each child of this element 
 foreach(Element child in e.Children)
 {
 // Call FormatCharacterRuns 
 FormatCharacterRuns(child);
 }
}

=20

Downloads

Template: FormLetterFormattingTemplate.doc

Output: Fo= rmLetterFormatted_out.doc

------=_Part_8418_852734709.1711645348651--