A document outline is a tree-like visualisation that provides an overview of the structure of the document. This can be used, e.g., to provide a list of chapters and subsections of a document.
By default, PDF documents created by VectSharp do not include an outline. If you wish to include an outline, you should create an OutlineTree object and provide it as an optional parameter to the SaveAsPDF method. The TopLevelItems property of the OutlineTree class contains a list of the top-level items in the outline, and each item can have zero or more Children.
Creating a basic outline
When creating an outline item, you can supply a tag to an element in the document; when the outline item is clicked, the document will scroll to the position of that element. You can also create an outline item without supplying a tag; in this case, the outline item will still be presented in the tree structure, but it will not be clickable.
The following example shows how to create a document with an outline.
Outlines in PDF can be customised: the colour of each heading can be specified, and the headings can be draw in bold, italics, or both. These settings can be specified while creating the outline tree. The following example shows how to create an outline tree specifying the format for each heading:
Unfortunately, most PDF viewers do not support formatted outlines. As of April 4th 2024, this is the software I tested:
Adobe Acrobat Reader supports both colours and text styles.
Mozilla Firefox only supports text styles, but not colours.
Google Chrome, Microsoft Edge, TeXworks, SumatraPDF, and the macOS Preview program support neither text styles nor colours.
Here is how the outline looks like in different software:
Creating an outline from a Markdown document
If you are creating a PDF document from a Markdown document using VectSharp.Markdown, you can use the headings in the Markdown document to automatically create an outline.
The MarkdownRenderer.Render and MarkdownRenderer.RenderSinglePage methods have an out parameter that returns a heading tree. You can supply this object to the static OutlineTree.CreateFromHeadings method, in order to receive an OutlineTree object that can be included in a PDF document. This method has an optional parameter topLevel, which can be used to exclude headings above a certain level from the outline. This is useful, for example, if the Markdown document has a single level 1 heading containing the document title.
The following example shows how to render the VectSharp readme as a PDF document including the outline.