PrintDocument for WinForms
目次
PrintDocument ライブラリ > 目次

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:

  1. Create an instance of the RenderToc class and add it to your document at the point where you want the TOC to appear.
  2. Add individual items (of the type RenderTocItem) to the RenderToc instance using the AddItem method, which provides the text of the item, the location it should point to, and optionally the level in the TOC.
  3. Insert RenderToc object into the document using the Insert method of the RenderObjectCollection class. The method uses two parameters, index, which indicates where to insert the object, and RenderObect which adds the object to insert, here the 'ToC'.

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