Question

I have created a flow document. In case the document is printed or saved to a file I would like to add a TOC to the start of the document.

What is the correct way of creating a TOC in a flowdocument? Can it be created automatically or should I add the TOC as the last thing looping through all the Paragraphs/Sections and manually add them to a table? If the manual creation is the way to go the next question that appears is how to determine the page number a Paragraph/Section is located on?

Was it helpful?

Solution

I did not find a way to do this automatically. But the following works:

  1. Reserve a page for the TOC
  2. Build the remaining document
  3. Insert the TOC on the reserved page and loop through the remaining sections and generate the TOC row by row. Page numbers of the sections are found using the DynamicDocumentPaginator

Note: If the TOC will span multiple pages more than one iteration is needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top