忽略TOC中的標題我正在生成一個文檔,其中包含標題以及嵌套節和子節的幾個級別。我想包括一個目錄。但是,除了指定的標題級別之外,TOC還包括標題。我如何從TOC中排除標題?使用Aspose.Words
這裏是我的代碼生成文件:
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 17.1.0。