Spire.Doc组件为用户提供了多种域类型选择,其中涵盖了Word自带的各种域类型。这些域类型被定义在枚举Spire.Doc.FieldType中,下图为部分域类型及描述:
该文我们详细介绍如何使用Spire.Doc添加下面三种常见的word 域:FieldDate(日期域),FieldIf (条件域)及TOC(目录域)。
FieldDate (日期域)
C#
//新建一个word文档对象
Document document = new Document();
//添加一个section和paragraph
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
//添加文本到paragraph
paragraph.AppendText("今天的日期为: ");
//添加日期域并设置日期格式
Field field = paragraph.AppendField("Date", FieldType.FieldData) as Field;
field.Code = @"DATE \@" + "\"yyyy年MM月dd日 \"";
//保存文档
document.SaveToFile("Sample.docx", FileFormat.Docx2013);
VB.NET
'新建一个word文档对象
Dim document As Document = New Document
'添加一个section和paragraph
Dim section As Section = document.AddSection
Dim paragraph As Paragraph = section.AddParagraph
'添加文本到paragraph
paragraph.AppendText("今天的日期为: ")
'添加日期域并设置日期格式
Dim field As Field = CType(paragraph.AppendField("Date", FieldType.FieldData),Field)
field.Code = ("DATE \@" + """yyyytMMdd """"")
'保存文档
document.SaveToFile("Sample.docx", FileFormat.Docx2013)
FieldIf(条件域)
Spire.Doc创建条件域的时候,我们会用到两个域,一个是条件域,一个是邮件合并域。格式为这样{IF { MERGEFIELD Count } > "60" "成绩合格" "成绩不合格"}
C#
{
//新建一个word文档对象并添加section和paragraph
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
//调用CreateIfField方法并赋值
CreateIfField(document, paragraph);
string[] fieldName = { "Count" };
string[] fieldValue = { "100" };
//合并值到条件域
document.MailMerge.Execute(fieldName, fieldValue);
//更新域
document.IsUpdateFields = true;
//保存文档
document.SaveToFile("sample.docx", FileFormat.Docx);
}
static void CreateIfField(Document document, Paragraph paragraph)
{
//添加条件域并设置条件
IfField ifField = new IfField(document);
ifField.Type = FieldType.FieldIf;
ifField.Code = "IF ";
paragraph.Items.Add(ifField);
paragraph.AppendField("Count",FieldType.FieldMergeField);
paragraph.AppendText(" > ");
paragraph.AppendText("\"60\" ");
paragraph.AppendText("\"成绩合格\" ");
paragraph.AppendText("\"成绩不合格\"");
IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);
(end as FieldMark).Type = FieldMarkType.FieldEnd;
paragraph.Items.Add(end);
ifField.End = end as FieldMark;
}
VB.NET
'新建一个word文档对象并添加section和paragraph
Dim document As Document = New Document
Dim section As Section = document.AddSection
Dim paragraph As Paragraph = section.AddParagraph
'调用CreateIfField方法并赋值
CreateIfField(document, paragraph)
Dim fieldName() As String
Dim fieldValue() As String
'合并值到条件域
document.MailMerge.Execute(fieldName, fieldValue)
'更新域
document.IsUpdateFields = true
'保存文档
document.SaveToFile("sample.docx", FileFormat.Docx)
Private Shared Sub CreateIfField(ByVal document As Document, ByVal paragraph As Paragraph)
'添加条件域并设置条件
Dim ifField As IfField = New IfField(document)
ifField.Type = FieldType.FieldIf
ifField.Code = "IF "
paragraph.Items.Add(ifField)
paragraph.AppendField("Count", FieldType.FieldMergeField)
paragraph.AppendText(" > ")
paragraph.AppendText("""60"" ")
paragraph.AppendText("\"成绩合格\" ")
paragraph.AppendText("\"成绩不合格\"")
Dim end As IParagraphBase = document.CreateParagraphItem(ParagraphItemType.FieldMark)
CType(end,FieldMark).Type = FieldMarkType.FieldEnd
paragraph.Items.Add(end)
ifField.End = CType(end,FieldMark)
End Sub
TOC(目录域)
Spire.Doc提供了AppendTOC()方法让用户能够直接添加目录域。
C#
{
//新建一个word文档对象并添加section和paragraph
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
//添加目录域
paragraph.AppendTOC(1, 3);
//添加内容到段落并设置样式
Paragraph para1 = section.AddParagraph();
para1.AppendText("Head1");
para1.ApplyStyle(BuiltinStyle.Heading1);
Paragraph para2 = section.AddParagraph();
para2.AppendText("Head2");
para2.ApplyStyle(BuiltinStyle.Heading2);
//更新目录域
document.UpdateTableOfContents();
//保存文档
document.SaveToFile("TOC.docx", FileFormat.Docx);
}
VB.NET
'新建一个word文档对象并添加section和paragraph
Dim document As Document = New Document
Dim section As Section = document.AddSection
Dim paragraph As Paragraph = section.AddParagraph
'添加目录域
paragraph.AppendTOC(1, 3)
'添加内容到段落并设置样式
Dim para1 As Paragraph = section.AddParagraph
para1.AppendText("Head1")
para1.ApplyStyle(BuiltinStyle.Heading1)
Dim para2 As Paragraph = section.AddParagraph
para2.AppendText("Head2")
para2.ApplyStyle(BuiltinStyle.Heading2)
'更新目录域
document.UpdateTableOfContents
'保存文档
document.SaveToFile("TOC.docx", FileFormat.Docx)