How to use mail merge to merge the text and image value

Ros8
Posted by in C# category on for Intermediate level | Points: 250 | Views : 434 red flag

Create a Word Document programmatically just by insert some text and image values by using the mail merge. I only want to copy the template content into the new document and keep a clean template that I can use it later.
Recommendation
Read Application program in JAVA at coimbatore before this article.

Introduction

Being a .NET programmer with a requirement to create a Word Document programmatically just by inserting some text and image values by using the mail merge. We only want to copy the template content into the new document and keep a clean template that we can reuse it later. And we don’t want to install Microsoft word or office to be installed on our machine, finally I found that the Free Spire.Doc can fulfill my requirements.  We can get the Spire.Doc.dll on .NET platform from Nuget and then easily add it as reference to our applications.


Steps of how to use the word mail merge and mail the text and image value to it.

Firstly, we need to have a clean word template. Please check my simple sample as below:


Step 1: Create a new word document and load the document from the file.

Document doc = new Document();

doc.LoadFromFile("Template.docx");

 

Step 2: Set the text value for the mail merge template by the fieldname and merge the specified value into template.

string[] fieldName = { "Name" };

string[] fieldValue = { "John" };

doc.MailMerge.Execute(fieldName, fieldValue);

 

Step 3: Set the image value for the mail merge template by the fieldname.

var fieldNames = new string[] { "MyImage" };

var fieldValues = new string[] { "logo.png" };

 Step 4: Create a method to get the image file:

void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs field)

 {

     string filePath = field.FieldValue as string;

     if (!string.IsNullOrEmpty(filePath))

     {

         field.Image = Image.FromFile(filePath);

     }

 

 }

 

Step 5: Call the method MailMerge_MergeImageField() to get the image and then merge the specified value into template.

doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);

doc.MailMerge.Execute(fieldNames, fieldValues);

 Step 6: Save the document to file.

doc.SaveToFile("result.docx", FileFormat.Docx);

 

Effective screenshot after merge the text and image value to the clean template:




Conclusion

Mail merge is widely used features for us to generate the reports, invoice and letters. It saves us lots of time and energy when we deal with large amounts of data.

Reference

http://www.e-iceblue.com/Knowledgebase/Spire.Doc/Spire.Doc-Program-Guide/Mail-Merge/How-to-use-mail-merge-to-merge-the-image-value.html

Recommendation
Read How to implement Remoting in ASP.NET after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Ros8
Full Name: Jane Qerge
Member Level: Starter
Member Status: Member
Member Since: 7/21/2016 11:06:18 PM
Country: United States



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)