Word格式处理控件Aspose.Words for .NET教程——使用DOM插入字段

Aspose.Words for .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

>>Aspose.Words for .NET已经更新至最新版,Font.EmphasisMark向公众公开,引入了MarkdownSaveOptions类,PDF版本1.5标记为过时,点击下载体验

了解更多Aspose文档管理产品  Aspose技术交流群(761297826)


使用DOM插入字段

在本文中,将讲解一下内容:

  • 使用DOM将合并字段插入文档中
  • 使用DOM将邮件合并地址块字段插入文档中
  • 在不使用DocumentBuilder的情况下将Advance字段插入文档
  • 在不使用DocumentBuilder的情况下将ASK字段插入文档
  • 在不使用DocumentBuilder的情况下将ASK字段插入文档
  • 在不使用DocumentBuilder的情况下将INCLUDETEXT字段插入文档
  • 在不使用DocumentBuilder的情况下将TOA字段插入文档

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

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

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

下例显示了如何使用DOM将合并字段添加到文档中的段落。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();
Document doc = new Document(dataDir + "in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

// Get paragraph you want to append this merge field to
Paragraph para = (Paragraph)doc.GetChildNodes(NodeType.Paragraph, true)[1];

// Move cursor to this paragraph
builder.MoveTo(para);

// We want to insert a merge field like this:
// { " MERGEFIELD Test1 \\b Test2 \\f Test3 \\m \\v" }

// Create instance of FieldMergeField class and lets build the above field code
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;

// Finally update this merge field
field.Update();

dataDir = dataDir + "InsertMergeFieldUsingDOM_out.doc";
doc.Save(dataDir);

使用DOM将邮件合并地址块字段插入文档中

ADDRESSBLOCK字段用于在Word文档中插入邮件合并地址块。Word文档中的ADDRESSBLOCK字段可以由FieldAddressBlock类表示。使用FieldAddressBlock类执行以下操作:

  • 指定是否在字段中包含国家/地区的名称。
  • 指定是否按照POST * CODE(通用邮政联盟2006)定义的收件人国家/地区来格式化地址。
  • 指定排除的国家/地区名称。
  • 指定名称和地址格式。
  • 指定用于格式化地址的语言ID。

下面的示例演示如何使用DOM向文档中的段落添加邮件合并地址块字段。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();
Document doc = new Document(dataDir + "in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

// Get paragraph you want to append this merge field to
Paragraph para = (Paragraph)doc.GetChildNodes(NodeType.Paragraph, true)[1];

// Move cursor to this paragraph
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\" }

// Create instance of FieldAddressBlock class and lets build the above field code
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";

// Finally update this merge field
field.Update();

dataDir = dataDir + "InsertMailMergeAddressBlockFieldUsingDOM_out.doc";
doc.Save(dataDir);

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

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

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

下面的示例演示如何使用DOM向文档中的段落添加高级字段。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();
Document doc = new Document(dataDir + "in.doc");
// Get paragraph you want to append this Advance field to
Paragraph para = (Paragraph)doc.GetChildNodes(NodeType.Paragraph, true)[1];

// We want to insert an Advance field like this:
// { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 \\x 100 \\y 100 }

// Create instance of FieldAdvance class and lets build the above field code
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";

// Finally update this Advance field
field.Update();

dataDir = dataDir + "InsertAdvanceFieldWithOutDocumentBuilder_out.doc";
doc.Save(dataDir);

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

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

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

下面的示例演示如何使用DOM向文档中的段落添加ASK字段。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();
Document doc = new Document(dataDir + "in.doc");
// Get paragraph you want to append this Ask field to
Paragraph para = (Paragraph)doc.GetChildNodes(NodeType.Paragraph, true)[1];

// We want to insert an Ask field like this:
// { ASK \"Test 1\" Test2 \\d Test3 \\o }

// Create instance of FieldAsk class and lets build the above field code
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;

// Finally update this Ask field
field.Update();

dataDir = dataDir + "InsertASKFieldWithOutDocumentBuilder_out.doc";
doc.Save(dataDir);

不使用DocumentBuilder将AUTHOR字段插入文档

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

  • 指定文档作者的姓名。

下面的示例演示如何使用DOM向文档中的段落添加AUTHOR字段。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();
Document doc = new Document(dataDir + "in.doc");
// Get paragraph you want to append this AUTHOR field to
Paragraph para = (Paragraph)doc.GetChildNodes(NodeType.Paragraph, true)[1];

// We want to insert an AUTHOR field like this:
// { AUTHOR Test1 }

// Create instance of FieldAuthor class and lets build the above field code
FieldAuthor field = (FieldAuthor)para.AppendField(FieldType.FieldAuthor, false);

// { AUTHOR Test1 }
field.AuthorName = "Test1";

// Finally update this AUTHOR field
field.Update();

dataDir = dataDir + "InsertAuthorField_out.doc";
doc.Save(dataDir);

不使用DocumentBuilder将INCLUDETEXT字段插入文档中

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

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

下面的示例演示如何使用DOM向文档中的段落添加INCLUDETEXT字段。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();
Document doc = new Document(dataDir + "in.doc");
// Get paragraph you want to append this INCLUDETEXT field to
Paragraph para = (Paragraph)doc.GetChildNodes(NodeType.Paragraph, true)[1];

// We want to insert an INCLUDETEXT field like this:
// { INCLUDETEXT  "file path" }

// Create instance of FieldAsk class and lets build the above field code
FieldIncludeText fieldIncludeText = (FieldIncludeText)para.AppendField(FieldType.FieldIncludeText, false);
fieldIncludeText.BookmarkName = "bookmark";
fieldIncludeText.SourceFullName = dataDir + @"IncludeText.docx";

doc.FirstSection.Body.AppendChild(para);

// Finally update this IncludeText field
fieldIncludeText.Update();

dataDir = dataDir + "InsertIncludeFieldWithoutDocumentBuilder_out.doc";
doc.Save(dataDir);

不使用DocumentBuilder将TOA字段插入文档中

TOA(权限表)字段将构建并插入一个权限表。TOA字段收集由TA(授权表条目)字段标记的条目。Microsoft Office Word中插入当您单击TOA场当局插入表格中的引文目录的上组参考选项卡。

下面的示例显示了如何使用DOM向文档中的段落添加TOA字段。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();
Document doc = new Document(dataDir + "in.doc");
// Get paragraph you want to append this TOA field to
Paragraph para = (Paragraph)doc.GetChildNodes(NodeType.Paragraph, true)[1];

// We want to insert TA and TOA fields like this:
// { TA  \c 1 \l "Value 0" }
// { TOA  \c 1 }

// Create instance of FieldAsk class and lets build the above field code
FieldTA fieldTA = (FieldTA)para.AppendField(FieldType.FieldTOAEntry, false);
fieldTA.EntryCategory = "1";
fieldTA.LongCitation = "Value 0";

doc.FirstSection.Body.AppendChild(para);

para = new Paragraph(doc);

// Create instance of FieldToa class
FieldToa fieldToa = (FieldToa)para.AppendField(FieldType.FieldTOA, false);
fieldToa.EntryCategory = "1";
doc.FirstSection.Body.AppendChild(para);

// Finally update this TOA field
fieldToa.Update();

dataDir = dataDir + "InsertTOAFieldWithoutDocumentBuilder_out.doc";
doc.Save(dataDir);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值