翻译|使用教程|编辑:李显亮|2020-07-01 10:14:16.423|阅读 383 次
概述:在Aspose.Words中,DocumentBuilder.InsertField方法用于在文档中插入新字段。本文将介绍如何使用DOM插入字段,包括将合并字段插入文档中、将邮件合并地址块字段插入文档中等。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
相关链接:
Aspose.Words for .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
>>Aspose.Words for .NET已经更新至v20.6,Font.EmphasisMark向公众公开,引入了MarkdownSaveOptions类,PDF版本1.5标记为过时,点击下载体验
在本文中,将讲解一下内容:
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);
ADDRESSBLOCK字段用于在Word文档中插入邮件合并地址块。Word文档中的ADDRESSBLOCK字段可以由FieldAddressBlock类表示。使用FieldAddressBlock类执行以下操作:
下面的示例演示如何使用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);
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);
“ 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);
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);
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);
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);
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn