没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:李显亮|2020-01-14 09:47:02.013|阅读 982 次
概述:本文将进入关于“样式处理”的介绍,在Aspose.Words中学会如何插入和使用目录字段。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
接下来我们将进入关于“样式处理”的介绍,在Aspose.Words中学会如何插入和使用目录字段。
>>Aspose.Words for .NET迎来2020第一次更新v20.1,支持插入LINQ Reporting Engine标签,点击下载体验
如何插入和使用目录字段
通常,我们会使用包含目录(TOC)的文档。使用Aspose.Words,可以插入自己的目录或仅用几行代码即可完全重建文档中的现有目录。本文概述了如何使用目录字段并演示了:
以编程方式插入目录
调用DocumentBuilder.InsertTableOfContents方法将TOC字段插入DocumentBuilder当前位置的文档中。 Word文档中的目录可以通过多种方式构建,并使用各种选项进行格式化。您将切换到该方法的字段切换,以控制表的构建方式和在文档中显示的方式。
在Microsoft Word中插入的目录中使用的默认开关是“ \ o” 1-3 \ h \ z \ u”。这些开关的说明以及受支持的开关的列表可在本文后面找到。可以使用该指南获取正确的开关,或者如果已经拥有包含想要的类似TOC的文档,则可以显示域代码(ALT + F9)并直接从该字段复制开关。下例显示了如何将目录字段插入文档。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
// Initialize document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table of contents at the beginning of the document.
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
// The newly inserted table of contents will be initially empty.
// It needs to be populated by updating the fields in the document.
doc.UpdateFields();
dataDir = dataDir + "DocumentBuilderInsertTOC_out.doc";
doc.Save(dataDir);
下面的示例演示如何使用标题样式作为条目将目录(TOC)插入文档。
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table of contents at the beginning of the document.
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
// Start the actual document content on the second page.
builder.InsertBreak(BreakType.PageBreak);
// Build a document with complex structure by applying different heading styles thus creating TOC entries.
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 1");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 1.1");
builder.Writeln("Heading 1.2");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 2");
builder.Writeln("Heading 3");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.1");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Writeln("Heading 3.1.1");
builder.Writeln("Heading 3.1.2");
builder.Writeln("Heading 3.1.3");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.2");
builder.Writeln("Heading 3.3");
doc.UpdateFields();
dataDir = dataDir + "DocumentBuilderInsertTableOfContents_out.doc";
doc.Save(dataDir);
该代码演示了新目录插入到空白文档中。然后,使用DocumentBuilder类插入具有适当标题样式的一些样本内容格式,这些样式用于标记要包含在TOC中的内容。接下来的几行通过更新文档的字段和页面布局来填充目录。

更新目录
Aspose.Words允许您仅用几行代码即可完全更新TOC。在对文档进行更改后,可以执行此操作以填充新插入的目录或更新现有目录。必须使用以下两种方法来更新文档中的TOC字段:
请注意,这两个更新方法必须按该顺序调用。如果反转,将填充目录,但不会显示页码。可以更新任意数量的不同目录。这些方法将自动更新文档中找到的所有目录。下例显示了如何通过调用字段更新来完全重建文档中的TOC字段。
doc。UpdateFields();
插入TC字段
可以通过调用DocumentBuilder.InsertField方法并将字段名称指定为“ TC”以及所需的任何开关,在DocumentBuilder的当前位置插入新的TC字段。下例显示了如何使用DocumentBuilder将TC字段插入文档中。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
// Initialize document.
Document doc = new Document();
// Create a document builder to insert content with.
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a TC field at the current document builder position.
builder.InsertField("TC \"Entry Text\" \\f t");
dataDir = dataDir + "DocumentBuilderInsertTCField_out.doc";
doc.Save(dataDir);
通常,为TOC指定特定的文本行,并用TC字段标记。最简单的方法在MS Word这样做是为了突出显示文本,然后按ALT + SHIFT + O 。这将使用所选文本自动创建TC字段。相同的技术可以通过代码来实现。下面的代码将查找与输入匹配的文本,并在与文本相同的位置插入TC字段。该代码基于本文中使用的相同技术。 下例显示了如何在文档的文本中查找和插入TC字段。
Document doc = new Document();
FindReplaceOptions options = new FindReplaceOptions();
// Highlight newly inserted content.
options.ApplyFont.HighlightColor = Color.DarkOrange;
options.ReplacingCallback = new InsertTCFieldHandler("Chapter 1", "\\l 1");
// Insert a TC field which displays "Chapter 1" just before the text "The Beginning" in the document.
doc.Range.Replace(new Regex("The Beginning"), "", options);
public sealed class InsertTCFieldHandler : IReplacingCallback
{
// Store the text and switches to be used for the TC fields.
private string mFieldText;
private string mFieldSwitches;
////// The switches to use for each TC field. Can be an empty string or null.
///public InsertTCFieldHandler(string switches)
: this(string.Empty, switches)
{
mFieldSwitches = switches;
}
////// The display text and switches to use for each TC field. Display name can be an empty string or null.
///public InsertTCFieldHandler(string text, string switches)
{
mFieldText = text;
mFieldSwitches = switches;
}
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
{
// Create a builder to insert the field.
DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document);
// Move to the first node of the match.
builder.MoveTo(args.MatchNode);
// If the user specified text to be used in the field as display text then use that, otherwise use the
// Match string as the display text.
string insertText;
if (!string.IsNullOrEmpty(mFieldText))
insertText = mFieldText;
else
insertText = args.Match.Value;
// Insert the TC field before this node using the specified string as the display text and user defined switches.
builder.InsertField(string.Format("TC \"{0}\" {1}", insertText, mFieldSwitches));
// We have done what we want so skip replacement.
return ReplaceAction.Skip;
}
}
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn




在使用Parasoft C/C++test执行BugDetective数据流分析时,可能会遇到用户自定义的资源API,那在这种情况下,若要判断是否存在资源问题,如资源泄露等,则需要手动配置测试配置。
大型SaaS系统的自动化测试常常受制于界面变化快、结构复杂、加载机制多变等因素。从元素识别到脚本管理,SmartBear TestComplete帮助Salesforce建了可靠的自动化测试体系。
BarTender 标签管理系统,正是帮助企业轻松实现 GS1 标准化标签设计、编码生成与信息联动的强大工具。
Parasoft C/C++test 是一款功能强大的 C/C++ 软件测试工具,集成了静态代码分析、单元测试、集成测试和覆盖率分析等功能,单元测试作为其关键功能之一,为了适配多样化的目标部署环境,C/C++test 设计了灵活的测试结果收集机制。通过Socket通讯方式来收集单元测试结果,从而扩展其测试覆盖范围与应用场景。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营