PrintDocument supports the automatic generation of table of contents (TOC). The RenderToc class is used to represent the table of contents in PrintDocument. This class is derived from RenderArea and adds TOC-specific features.
The individual items within the TOC are represented by the RenderTocItem (derived from RenderParagraph). Each TOC item holds a hyperlink pointing to a location in the document so, the same mechanism is used for connecting TOC items to document content as for the hyperlinks.
The GIF below depicts the table of contents feature in PrintDocument.

To add a table of contents to your document, do the following:
The code snippet below shows how to add Table of Contents to a PritnDocument.
| C# |
コードのコピー
|
|---|---|
// RenderTocオブジェクトのインスタンスを作成します。 RenderToc toc = new RenderToc(); toc.BreakAfter = BreakEnum.Page; // チャプターをループします。 for (int c = 1; c < chapterCount; c++) { // 各チャプターはRenderAreaオブジェクトとして表されます。 RenderArea chapter = new RenderArea(); if (c < chapterCount - 1) chapter.BreakAfter = BreakEnum.Page; RenderText chapterTitle = new RenderText(string.Format("Chapter {0}", c), chapterTitleStyle); chapter.Children.Add(chapterTitle); chapter.Children.Add(new RenderText(chapterIntroduction.ToString(), AlignHorzEnum.Justify)); // チャプターのアイテムをRenderTocに追加します。 toc.AddItem(chapterTitle.Text, chapterTitle, 1); // 現在のチャプターの部分をループします。 for (int p = 1; p < partCount; p++) { RenderText partTitle = new RenderText(string.Format("Chapter {0} part {1}", c, p), partTitleStyle); chapter.Children.Add(partTitle); chapter.Children.Add(new RenderText(partContent.ToString(), AlignHorzEnum.Justify)); // チャプターパートのアイテムをRenderTocに追加します。 toc.AddItem(string.Format("Part {0}", p), partTitle, 2); } // ドキュメントにチャプターを追加します。 doc.Body.Children.Add(chapter); } // RenderTocをドキュメントのタイトルの直後に挿入します。 doc.Body.Children.Insert(1, toc); |
|