插入字段

有几种不同的方法可以将字段插入到文档中:

在本文中,我们将更详细地了解每种方式,并分析如何使用这些选项插入某些字段。

使用 DocumentBuilder 将字段插入文档

在 Aspose.Words 中,InsertField 方法用于将新字段插入到文档中。第一个参数接受要插入的字段的完整字段代码。第二个参数是可选的,允许手动设置字段的字段结果。如果未提供此字段,则该字段会自动更新。您可以将 null 或empty 传递给此参数以插入具有空字段值的字段。如果您不确定特定的字段代码语法,请先在 Microsoft Word 中创建字段,然后切换以查看其字段代码。

以下代码示例演示如何使用 DocumentBuilder 将合并字段插入到文档中:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(@"MERGEFIELD MyFieldName \* MERGEFORMAT");
doc.Save(ArtifactsDir + "WorkingWithFields.InsertField.docx");

使用相同的技术来插入嵌套在其他字段中的字段。

以下代码示例演示如何使用 DocumentBuilder 插入嵌套在另一个字段中的字段:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 0; i < 5; i++)
builder.InsertBreak(BreakType.PageBreak);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
// We want to insert a field like this:
// { IF {PAGE} <> {NUMPAGES} "See Next Page" "Last Page" }
Field field = builder.InsertField(@"IF ");
builder.MoveTo(field.Separator);
builder.InsertField("PAGE");
builder.Write(" <> ");
builder.InsertField("NUMPAGES");
builder.Write(" \"See Next Page\" \"Last Page\" ");
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertNestedFields.docx");

在字段级别指定区域设置

语言标识符是一个国家或地理区域的语言的标准国际数字缩写。使用 Aspose.Words,您可以使用 LocaleId 属性在字段级别指定区域设置,该属性获取或设置字段的区域设置 ID。

以下代码示例展示了如何使用此选项:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
DocumentBuilder builder = new DocumentBuilder();
Field field = builder.InsertField(FieldType.FieldDate, true);
field.LocaleId = 1049;
builder.Document.Save(ArtifactsDir + "WorkingWithFields.SpecifylocaleAtFieldlevel.docx");

插入无类型/空字段

如果您想像 Microsoft Word 允许的那样插入非类型化/空字段 ({}),您可以使用带有 FieldType.FieldNone 参数的 InsertField 方法。要在 Word 文档中插入字段,可以按"Ctrl + F9"组合键。

以下代码示例展示了如何将空字段插入到文档中:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
FieldUnknown field = (FieldUnknown) builder.InsertField(FieldType.FieldNone, false);
doc.Save(ArtifactsDir + "WorkingWithFields.InsertFieldNone.docx");

使用 FieldBuilder 将字段插入文档

在 Aspose.Words 中插入字段的另一种方法是 FieldBuilder 类。它提供流畅的界面来指定字段开关和参数值作为文本、节点甚至嵌套字段。

以下代码示例演示如何使用 FieldBuilder 将字段插入到文档中:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
// Prepare IF field with two nested MERGEFIELD fields: { IF "left expression" = "right expression" "Firstname: { MERGEFIELD firstname }" "Lastname: { MERGEFIELD lastname }"}
FieldBuilder fieldBuilder = new FieldBuilder(FieldType.FieldIf)
.AddArgument("left expression")
.AddArgument("=")
.AddArgument("right expression")
.AddArgument(
new FieldArgumentBuilder()
.AddText("Firstname: ")
.AddField(new FieldBuilder(FieldType.FieldMergeField).AddArgument("firstname")))
.AddArgument(
new FieldArgumentBuilder()
.AddText("Lastname: ")
.AddField(new FieldBuilder(FieldType.FieldMergeField).AddArgument("lastname")));
// Insert IF field in exact location
Field field = fieldBuilder.BuildAndInsert(doc.FirstSection.Body.FirstParagraph);
field.Update();
doc.Save(ArtifactsDir + "Field.InsertFieldUsingFieldBuilder.docx");

使用 DOM 插入字段

您还可以使用 Aspose.Words Document Object Model (DOM) 插入各种类型的字段。在本节中,我们将看一些示例。

使用 DOM 将合并字段插入到文档中

Word文档中的MERGEFIELD字段可以用FieldMergeField类来表示。您可以使用 FieldMergeField 类执行以下操作:

  • 指定合并字段的名称
  • 指定合并字段的格式
  • 指定合并字段的字段分隔符和字段结尾之间的文本
  • 如果字段不为空,则指定要在合并字段后插入的文本
  • 如果字段不为空,则指定要在合并字段之前插入的文本

以下代码示例演示如何使用 DOM 将 MERGE 字段添加到文档中的段落:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0];
builder.MoveTo(para);
// We want to insert a merge field like this:
// { " MERGEFIELD Test1 \\b Test2 \\f Test3 \\m \\v" }
FieldMergeField field = (FieldMergeField) builder.InsertField(FieldType.FieldMergeField, false);
// { " MERGEFIELD Test1" }
field.FieldName = "Test1";
// { " MERGEFIELD Test1 \\b Test2" }
field.TextBefore = "Test2";
// { " MERGEFIELD Test1 \\b Test2 \\f Test3 }
field.TextAfter = "Test3";
// { " MERGEFIELD Test1 \\b Test2 \\f Test3 \\m" }
field.IsMapped = true;
// { " MERGEFIELD Test1 \\b Test2 \\f Test3 \\m \\v" }
field.IsVerticalFormatting = true;
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertMergeFieldUsingDOM.docx");

使用 DOM 将 Mail Merge ADDRESSBLOCK 字段插入文档中

ADDRESSBLOCK字段用于在Word文档中插入mail merge地址块。 Word文档中的ADDRESSBLOCK字段可以用FieldAddressBlock类来表示。您可以使用 FieldAddressBlock 类执行以下操作:

  • 指定字段中是否包含国家/地区名称
  • 指定是否根据 POST*CODE 定义的收件人国家/地区来格式化地址(Universal Postal Union 2006)
  • 指定排除的国家/地区名称
  • 指定姓名和地址格式
  • 指定用于格式化地址的语言ID

以下代码示例演示如何使用 DOM 将 Mail Merge ADDRESSBLOCK 字段添加到文档中的段落:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0];
builder.MoveTo(para);
// We want to insert a mail merge address block like this:
// { ADDRESSBLOCK \\c 1 \\d \\e Test2 \\f Test3 \\l \"Test 4\" }
FieldAddressBlock field = (FieldAddressBlock) builder.InsertField(FieldType.FieldAddressBlock, false);
// { ADDRESSBLOCK \\c 1" }
field.IncludeCountryOrRegionName = "1";
// { ADDRESSBLOCK \\c 1 \\d" }
field.FormatAddressOnCountryOrRegion = true;
// { ADDRESSBLOCK \\c 1 \\d \\e Test2 }
field.ExcludedCountryOrRegionName = "Test2";
// { ADDRESSBLOCK \\c 1 \\d \\e Test2 \\f Test3 }
field.NameAndAddressFormat = "Test3";
// { ADDRESSBLOCK \\c 1 \\d \\e Test2 \\f Test3 \\l \"Test 4\" }
field.LanguageId = "Test 4";
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertMailMergeAddressBlockFieldUsingDOM.docx");

在不使用 DocumentBuilder 的情况下将 ADVANCE 字段插入到文档中

ADVANCE 字段用于将行中的后续文本向左、向右、向上或向下偏移。 Word文档中的ADVANCE字段可以用FieldAdvance类来表示。您可以使用 FieldAdvance 类执行以下操作:

  • 指定字段后面的文本应从页面顶部边缘垂直移动的点数
  • 指定字段后面的文本应从列、框架或文本框的左边缘水平移动的点数
  • 指定字段后面的文本应向左、向右、向上或向下移动的点数

以下代码示例演示如何使用 DOM 将 ADVANCE 字段添加到文档中的段落:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0];
// We want to insert an Advance field like this:
// { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 \\x 100 \\y 100 }
FieldAdvance field = (FieldAdvance) para.AppendField(FieldType.FieldAdvance, false);
// { ADVANCE \\d 10 " }
field.DownOffset = "10";
// { ADVANCE \\d 10 \\l 10 }
field.LeftOffset = "10";
// { ADVANCE \\d 10 \\l 10 \\r -3.3 }
field.RightOffset = "-3.3";
// { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 }
field.UpOffset = "0";
// { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 \\x 100 }
field.HorizontalPosition = "100";
// { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 \\x 100 \\y 100 }
field.VerticalPosition = "100";
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertAdvanceFieldWithOutDocumentBuilder.docx");

在不使用 DocumentBuilder 的情况下将 ASK 字段插入到文档中

ASK 字段用于提示用户输入要分配给 Word 文档中的书签的文本。 Word文档中的ASK字段可以用FieldAsk类来表示。您可以使用 FieldAsk 类执行以下操作:

  • 指定书签的名称
  • 指定默认用户响应(提示窗口中包含的初始值)
  • 指定每个 Mail Merge 操作是否应接收一次用户响应
  • 指定提示文本(提示窗口的标题)

以下代码示例演示如何使用 DOM 将 ASK 字段添加到文档中的段落:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0];
// We want to insert an Ask field like this:
// { ASK \"Test 1\" Test2 \\d Test3 \\o }
FieldAsk field = (FieldAsk) para.AppendField(FieldType.FieldAsk, false);
// { ASK \"Test 1\" " }
field.BookmarkName = "Test 1";
// { ASK \"Test 1\" Test2 }
field.PromptText = "Test2";
// { ASK \"Test 1\" Test2 \\d Test3 }
field.DefaultResponse = "Test3";
// { ASK \"Test 1\" Test2 \\d Test3 \\o }
field.PromptOnceOnMailMerge = true;
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertASKFieldWithOutDocumentBuilder.docx");

在不使用 DocumentBuilder 的情况下将 AUTHOR 字段插入到文档中

AUTHOR 字段用于指定 Document 属性中文档作者的姓名。 Word文档中的AUTHOR字段可以用FieldAuthor类来表示。您可以使用 FieldAuthor 类执行以下操作:

  • 指定文档作者的姓名

以下代码示例演示如何使用 DOM 将 AUTHOR 字段添加到文档中的段落:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0];
// We want to insert an AUTHOR field like this:
// { AUTHOR Test1 }
FieldAuthor field = (FieldAuthor) para.AppendField(FieldType.FieldAuthor, false);
field.AuthorName = "Test1"; // { AUTHOR Test1 }
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertAuthorField.docx");

在不使用 DocumentBuilder 的情况下将 INCLUDETEXT 字段插入到文档中

INCLUDETEXT 字段插入字段代码中指定的文档中包含的文本和图形。您可以插入整个文档或书签引用的文档的一部分。 Word文档中的该字段由INCLUDETEXT表示。您可以使用 FieldIncludeText 类执行以下操作:

  • 指定所包含文档的书签名称
  • 指定文档的位置

以下代码示例演示如何使用 DOM 将 INCLUDETEXT 字段添加到文档中的段落:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Paragraph para = new Paragraph(doc);
// We want to insert an INCLUDETEXT field like this:
// { INCLUDETEXT "file path" }
FieldIncludeText fieldIncludeText = (FieldIncludeText) para.AppendField(FieldType.FieldIncludeText, false);
fieldIncludeText.BookmarkName = "bookmark";
fieldIncludeText.SourceFullName = MyDir + "IncludeText.docx";
doc.FirstSection.Body.AppendChild(para);
fieldIncludeText.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertIncludeFieldWithoutDocumentBuilder.docx");

在不使用 DocumentBuilder 的情况下将 TOA 字段插入到文档中

TOA权威表)字段构建并插入权威表。 TOA 字段收集由 TA权限表条目)字段标记的条目。当您在 References 选项卡上的 权限表 组中单击"插入权限表"时,Microsoft Office Word 会插入 TOA 字段。当您查看文档中的 TOA 字段时,语法如下所示:

{ TOA [Switches ] }

以下代码示例演示如何使用 DOM 将 TOA 字段添加到文档中的段落:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Paragraph para = new Paragraph(doc);
// We want to insert TA and TOA fields like this:
// { TA \c 1 \l "Value 0" }
// { TOA \c 1 }
FieldTA fieldTA = (FieldTA) para.AppendField(FieldType.FieldTOAEntry, false);
fieldTA.EntryCategory = "1";
fieldTA.LongCitation = "Value 0";
doc.FirstSection.Body.AppendChild(para);
para = new Paragraph(doc);
FieldToa fieldToa = (FieldToa) para.AppendField(FieldType.FieldTOA, false);
fieldToa.EntryCategory = "1";
doc.FirstSection.Body.AppendChild(para);
fieldToa.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertTOAFieldWithoutDocumentBuilder.docx");