I would like to add a Table of Contents to a document I create with MigraDoc. My code looks like this:
Code:
private void AddRefFields(PageViewModel page, Section sec)
{
foreach (var item in page.GetReportSections().Where(s => s != null).OrderBy(reportSection => reportSection.OrderIndex))
{
var par = sec.AddParagraph();
par.AddText(item.SecondaryLangTitle + "\t\t\t\t");
par.AddPageRefField(item.SecondaryLangTitle);
}
foreach (var child in page.Children)
{
AddRefFields(child, sec);
}
}
The problem is that the pages are not ordered, so I get something like this:
SectionA 123
Section B 89
Section C 249
Section D 152
It seems like it is not possible to access the page numbers in the code, so how can I create an ordered table of contents?