简体   繁体   中英

Ignore the title in TOC using Aspose.Words

I am generating a document that contains a title as well as several levels of nested sections and sub-sections. I want to include a table of contents. However, in addition to the Heading levels specified, the TOC is also including the title. How can I exclude the title from the TOC?

Here is my code to generate the document:

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"));
}

Notice that Document Title is included in the TOC:

使用 Aspose.Words 在 TOC 中显示标题的屏幕截图

I am using Aspose.Words version 17.1.0.

There appears to be a bug in Aspose.Words that treats Title styles the same as Heading 1 styles for the purposes of generating the TOC. A workaround is to instead use the \\t switch for applying TOC formatting to custom styles. We can then specify Heading 1 as the name of the 'custom' style.

    // \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");

This query was already answered in Aspose.Words forum below:

Ignore the title in TOC

However, I am copying the message below for your reference:

Please try running the following code and see if it meets your requirement?

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");

Hope, this helps. I work with Aspose as Developer Evangelist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM