代码之家  ›  专栏  ›  技术社区  ›  djs Evandro Pomatti

使用Aspose.Words忽略TOC中的标题

  •  1
  • djs Evandro Pomatti  · 技术社区  · 9 年前

    我正在生成一个文档,其中包含一个标题以及多个级别的嵌套节和子节。我想包括一个目录。然而,除了指定的标题级别外,TOC还包括标题。如何从TOC中排除标题?

    下面是我生成文档的代码:

    public void DocWithTOC()
    {
        // start with a blank document
        var doc = new Document();
        var builder = new DocumentBuilder(doc);
    
        // add a title. this should not be in the TOC.
        builder.CurrentParagraph.AppendChild(new Run(doc) { Text = "Document Title" } );
        builder.CurrentParagraph.ParagraphFormat.StyleIdentifier = StyleIdentifier.Title;
    
        // add TOC
        builder.InsertTableOfContents("\\o \"1-1\" \\h \\z \\u"); // \o "1-1" --> only apply TOC to Heading 1 elements
    
        // add first section (heading1). this should be in the TOC.
        var para = builder.InsertParagraph();
        para.AppendChild(new Run(doc) { Text = "Section 1" });
        para.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
    
        // add first section content.
        para = builder.InsertParagraph();
        para.AppendChild(new Run(doc) { Text = "This is the content under the first section. The header is included in the TOC." });
        para.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
    
        // add a sub-section (heading2). should not be in TOC.
        para = builder.InsertParagraph();
        para.AppendChild(new Run(doc) { Text = "Subsection 1.1" });
        para.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
    
        // add first sub-section content.
        para = builder.InsertParagraph();
        para.AppendChild(new Run(doc) { Text = "This is the content under the first sub-section of the first section. The header is NOT in the TOC." });
        para.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
    
        // add second section. this should be in the TOC.
        para = builder.InsertParagraph();
        para.AppendChild(new Run(doc) { Text = "Section 2" });
        para.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
    
        // add second section content.
        para = builder.InsertParagraph();
        para.AppendChild(new Run(doc) { Text = "The second section also has content. The header is included in the TOC." });
        para.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
    
        // apply TOC via Aspose.Words API
        doc.UpdateFields();
    
        // save to My Documents folder
        var myDocsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
        doc.Save(Path.Combine(myDocsPath, "AsposeTOC.docx"));
    }
    

    注意 Document Title 包含在TOC中:

    Screenshot showing title in TOC using Aspose.Words

    我正在使用阿斯波塞。Word版本17.1.0。

    2 回复  |  直到 9 年前
        1
  •  1
  •   djs Evandro Pomatti    9 年前

    Aspose中似乎存在错误。对待的话 Title 样式与 Heading 1 用于生成TOC的样式。解决方法是使用 \t switch 用于将TOC格式应用于自定义样式。然后,我们可以指定 标题1 作为“自定义”样式的名称。

        // \t "Heading 1, 1" --> custom formatting to treat Heading 1 styles as level 1 elements in the TOC
        builder.InsertTableOfContents("\\h \\z \\t \"Heading 1, 1");
    
        2
  •  0
  •   Awais Hafeez    9 年前

    此查询已在Aspose中回答。论坛文字如下:

    Ignore the title in TOC

    但是,我复制了以下消息供您参考:

    请尝试运行以下代码,看看它是否满足您的要求?

    Document doc = new Document(MyDir + @"AsposeTOC.docx");
    DocumentBuilder builder = new DocumentBuilder(doc);
    
    builder.MoveToDocumentEnd();
    builder.Writeln();
    builder.Writeln();
    
    builder.InsertTableOfContents("TOC \\o \"2 - 3\" \\h \\z \\t \"Heading 1, 1");
    
    doc.UpdateFields();
    
    doc.Save(MyDir + @"17.1.0.docx");
    

    希望这有帮助。我与Aspose合作,担任开发人员布道者