Aspose.Words for .NET样式处理教程——如何插入和使用目录字段

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

接下来我们将进入关于“样式处理”的介绍,在Aspose.Words中学会如何插入和使用目录字段。

>>Aspose.Words for .NET最新下载,支持插入LINQ Reporting Engine标签,点击下载体验 

欢迎下载|体验更多Aspose文档管理产品或 加入Aspose技术交流群(761297826)


如何插入和使用目录字段

通常,我们会使用包含目录(TOC)的文档。使用Aspose.Words,可以插入自己的目录或仅用几行代码即可完全重建文档中的现有目录。本文概述了如何使用目录字段并演示了:

  • 如何插入全新的目录
  • 更新文档中的新目录或现有目录。
  • 指定开关以控制目录的格式和整体结构。
  • 如何修改目录的样式和外观。
  • 如何从文档中删除整个TOC字段以及所有条目。

以编程方式插入目录

调用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字段:

  • Document.UpdateFields
  • Document.UpdatePageLayout

请注意,这两个更新方法必须按该顺序调用。如果反转,将填充目录,但不会显示页码。可以更新任意数量的不同目录。这些方法将自动更新文档中找到的所有目录。下例显示了如何通过调用字段更新来完全重建文档中的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;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值