Tuesday, 26 July 2011 07:05

How to Use Mail Merge for Silverlight

Written by support iceblue


Mail merge is often used to print reports in bulk, such as financial statement, payroll or transcript. And the merged documents can be sent by E-mail.

In this article, I will show one way to generate mail merged reports via Spire.Doc.



We need to finish the following 3 steps to generate our report.

  • Create a mail merge template.
  • Load data from database.
  • Merge data into template and save.

Every step includes several sub-steps and in #2 and #3 we need to write some code.


Create mail merge template

A template is a reusable document. It renders the pattern of our report. We could modify it to change our report without any modification of code.

Note: in this section, all tables mean DataTable instance, not physical table in database.


First, We can create the template in MS Word or by other program. It is the template we need to create. Data will be filled in the red party.

Mail Merge Fax

Second, Insert mail-merge-field as placeholder into the red-block. There are three types of mail-merge-field:

  • GeneralField is a general Word mail-merge-field. It is real data field and our data will be filled in it during merge process.
  • TableField is assistant mail-merge-field and used as a container of multiple related GeneralFields and other TableFields. So it is not data placeholder and no data will be filled in. It is composed of two special mail-merge-fields: TableStart:TableName and TableEnd:TableName. During merge process, the data of related GeneralFields contained by one same TableField will be from one same data table.
  • GroupField is assistant mail-merge-field too. It can contain multiple related GeneralFields and TableFields. It is composed of two special mail-merge-fields: GroupStart:GroupName and GroupEnd:GroupName. During merge process, all Word document elements included in a GroupField will be copied. One row in data table has one copy and data in the row will be filled into the fields in the copy. If the row has sub data table, the data in sub data table will be filled into the fields included in the corresponding TableField. If the sub data table has multiple data rows, the corresponding TableField will be copied and filled too. We need to insert a mail-merge-field named GroupStart:Order in the top of the template body and insert a mail-merge-field named GroupEnd:OrderM in the bottom of the template body.

In this Silverlight application we only use GeneralField. As the image shows below. Insert mail-merge-field as placeholder into the red-block.

Mail Merge Fax

Load Data

Spire.Doc provides merge data from various kind of datasources. This program merge data from a string array which is filled by user.

Mail Merge Fax

String[] fieldNames
                = new String[] { "Contact Name", "Fax", "From", "Date", "Subject", "Content" };
            DateTime faxDate
                = this.datePickerFaxDate.SelectedDate.HasValue ? 
                    this.datePickerFaxDate.SelectedDate.Value : DateTime.Now;
            String[] fieldValues
                = new String[] 


Merge data into template and save

In this section, we need to write some code to call Spire.Doc to merge our data and template.

this.documentTemplate.MailMerge.Execute(fieldNames, fieldValues);

            bool? result = this.saveFileDialog.ShowDialog();
            if (result.HasValue && result.Value)
                using (Stream stream = this.saveFileDialog.OpenFile())
                    //this.documentTemplate.SaveToStream(stream, FileFormat.Doc);
                    this.documentTemplate.SaveToFile(stream, FileFormat.Doc);


The Result

Mail Merge Fax


Monday, 18 July 2011 05:40

How to Convert Word to RTF

Written by support iceblue

Why Convert Word to RTF?

Since 1990s, Microsoft began to use .doc extension for a range of their proprietary word processing formats. Doc format is usually associated with Microsoft Office applications. But storing text in Doc files is not always convenient, as they are not compatible with the majority of third-party text editors. To make Doc information more accessible, it is better to store it in other, more common format. And as a rule, users select RTF format, because it is supported by the wide range of applications.

How to Convert Word to RTF with Spire.Doc?

Spire.Doc can convert Word to RTF without Microsoft Office installed on your system. Download Spire.Doc (or Spire.Office) with .NET Framework 2.0 (or above) and follow the simple steps below to convert Word to RTF.

Step 1

Create a project through Visual Studio and add Spire.Doc DLL as reference.

Step 2

Load local Word doc file which we need to convert to RTF format. Use the code below to load Word file.

            Document document = new Document();

Step 3

Spire.Doc allows users to convert Word doc files to most of popular file formats including PDF, HTML, RTF, EPub, XML, Dot, Text, etc. The following code can help us convert Word to RTF.

document.SaveToFile("Sample.rtf", FileFormat.Rtf);

Step 4

Copy the full code below into the project and start the project by pressing F5.

Convert Word to RTF with C# Code:

	private void button1_Click(object sender, EventArgs e)
	    //Create word document
	    Document document = new Document();

	    //Save doc file.
	    document.SaveToFile("Sample.rtf", FileFormat.Rtf);

	    //Launching the MS Word file.

	private void WordDocViewer(string fileName)
	    catch { }

Convert Word to RTF with VB.NET Code:

	Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
	    'Create word document
	    Dim document_Renamed As New Document()

	    'Save doc file.
	    document_Renamed.SaveToFile("Sample.rtf", FileFormat.Rtf)

	    'Launching the MS Word file.
	End Sub

	Private Sub WordDocViewer(ByVal fileName As String)
	    End Try
	End Sub        


Effective Screenshot:

Word to RTF

Friday, 15 July 2011 06:36

How to Convert XML to Word

Written by support iceblue

Why Use C#/VB.NET Convert Office OpenXML to Word?

As a great programming language, Office OpenXML is hard to navigate and access when in its natural form. And converting files from Office OpenXML to Word form can help to find and alter parts of code. On the other hand, converting Office OpenXML to Word is helpful in sharing the code with others.

How to Use C#/VB.NET Convert Office OpenXML to Word?

Spire.Doc allows users to convert files from Office OpenXML to Word with C#/VB.NET. Spire.Doc is a MS Word component which enables user to perform a wide range of Word document processing tasks directly for .NET and Silverlight. Like Microsoft Word can help you save Word files to different format, Spire.Doc also support converting files from Word to HTML, PDF, Office OpenXML, RTF, Text, Docx, Dot, etc. Furthermore, besides converting files from Word to other popular file format, Spire.Doc can also transfer files from Office OpenXML to Word, RTF to Word, Text to Word, Dot to Word, etc.

Download Spire.Doc (or Spire.Office) with .NET framework together and use the C#/VB.NET code below to convert Office OpenXML to Word.

Office OpenXML to Word:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;

namespace Xml2Word
    class Program
        static void Main(string[] args)
            Document doc = new Document();
            doc.LoadFromFile("sample.xml", FileFormat.Xml);
            doc.SaveToFile("test.doc", FileFormat.Doc);


Office OpenXML to Word:

Imports System.Text
Imports Spire.Doc

Namespace Xml2Word
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim doc As New Document()
			doc.LoadFromFile("sample.xml", FileFormat.Xml)
			doc.SaveToFile("test.doc", FileFormat.Doc)
		End Sub
	End Class
End Namespace