翻譯|使用教程|編輯:李顯亮|2020-07-01 10:14:16.423|閱讀 464 次
概述:在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@fc6vip.cn