簡體   English   中英

使用 Aspose.Words 忽略 TOC 中的標題

[英]Ignore the title in TOC using Aspose.Words

我正在生成一個包含標題以及多個級別的嵌套部分和子部分的文檔。 我想包括一個目錄。 但是,除了指定的標題級別之外,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 中:

使用 Aspose.Words 在 TOC 中顯示標題的屏幕截圖

我正在使用 Aspose.Words 17.1.0 版。

Aspose.Words 中似乎存在一個錯誤,為了生成 TOC,將Title樣式與Heading 1樣式相同。 一種解決方法是使用\\t 開關將 TOC 格式應用於自定義樣式。 然后我們可以將Heading 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");

此查詢已在以下 Aspose.Words 論壇中得到解答:

忽略 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 合作,擔任開發人員布道師

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM