本文将介绍如何使用Spire.Doc组件创建Word邮件合并模板文档,以及如何合并文本和图片到已有的模板文档。
创建邮件合并模板文档
C#
//创建Document实例
Document document = new Document();
//添加节
Section section = document.AddSection();
//添加段落
Paragraph paragraph = section.AddParagraph();
//添加文本
paragraph.AppendText("\n姓 名 : ");
//添加合并域“Name”
paragraph.AppendField("Name", FieldType.FieldMergeField);
//添加文本
paragraph.AppendText("\n电 话 : ");
//添加合并域"Phone"
paragraph.AppendField("Phone", FieldType.FieldMergeField);
//添加文本
paragraph.AppendText("\n部 门 : ");
//添加合并域“Department”
paragraph.AppendField("Department", FieldType.FieldMergeField);
//添加文本
paragraph.AppendText("\n照 片 : ");
//添加合并域”Photo”
paragraph.AppendField("Image:Photo", FieldType.FieldMergeField);
//保存并关闭文档
document.SaveToFile("模板.docx", FileFormat.Docx2013);
VB.NET
'创建Document实例
Dim document As Document = New Document
'添加节
Dim section As Section = document.AddSection
'添加段落
Dim paragraph As Paragraph = section.AddParagraph
'添加文本
paragraph.AppendText("\n姓 名 :")
'添加合并域“Name”
paragraph.AppendField("Name", FieldType.FieldMergeField)
'添加文本
paragraph.AppendText("\n电 话 :")
'添加合并域"Phone"
paragraph.AppendField("Phone", FieldType.FieldMergeField)
'添加文本
paragraph.AppendText("\n部 门 :")
'添加合并域“Department”
paragraph.AppendField("Department", FieldType.FieldMergeField)
'添加文本
paragraph.AppendText("\n照 片 :")
'添加合并域”Photo”
paragraph.AppendField("Image:Photo", FieldType.FieldMergeField)
'保存并关闭文档
document.SaveToFile("模板.docx ", FileFormat.Docx2013)
合并文本和图片到模板
C#
//载入模板文档
Document doc = new Document();
doc.LoadFromFile(@"模板.docx");
var textFieldNames = new string[] { "Name", "Phone", "Department" };
var textFieldValues = new string[] { "李 晓 飞", "15581678910", "技 术 支 持" };
var imageFieldNames = new string[] { "Photo" };
var imageFieldValues = new string[] { "image.jpg" };
//合并文本到模板
doc.MailMerge.Execute(textFieldNames, textFieldValues);
//创建合并图片自定义事件
doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);
//合并图片到模板
doc.MailMerge.Execute(imageFieldNames, imageFieldValues);
//保存文档
doc.SaveToFile("result.docx", FileFormat.Docx);
//载入图片
static void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs field)
{
string filePath = field.FieldValue as string;
if (!string.IsNullOrEmpty(filePath))
{
field.Image = Image.FromFile(filePath);
}
}
VB.NET
'载入模板文档
Dim doc As Document = New Document
doc.LoadFromFile("模板.docx")
Dim textFieldNames As var = New String() {"Name", "Phone", "Department"}
Dim textFieldValues As var = New String() {"李 晓 飞", "15581678910", "技 术 支 持"}
Dim imageFieldNames As var = New String() {"Photo"}
Dim imageFieldValues As var = New String() {"image.jpg"}
'合并文本到模板
doc.MailMerge.Execute(textFieldNames, textFieldValues)
'创建合并图片自定义事件
AddHandler doc.MailMerge.MergeImageField, AddressOf Me.MailMerge_MergeImageField
'合并图片到模板
doc.MailMerge.Execute(imageFieldNames, imageFieldValues)
'保存文档
doc.SaveToFile("result.docx", FileFormat.Docx)
'载入图片
Private Shared Sub MailMerge_MergeImageField(ByVal sender As Object, ByVal field As MergeImageFieldEventArgs)
Dim filePath As String = CType(field.FieldValue,String)
If Not String.IsNullOrEmpty(filePath) Then
field.Image = Image.FromFile(filePath)
End If
End Sub