Hello,
1. We need to generate PDF document with the hierarchical TOC (let us say 2 levels). Here it is our sample code:
Document document = new Document();
Page tocPage = document.Pages.Add();
tocPage.PageInfo = new PageInfo
{
Margin = new MarginInfo { Bottom = 0, Left = 0, Right = 0, Top = 0 }
};
tocPage.TocInfo = new TocInfo()
{
Title = new TextFragment(“Table of Contents”),
};
Page docPage = document.Pages.Add();
docPage.PageInfo = new PageInfo
{
Margin = new MarginInfo { Bottom = 0, Left = 0, Right = 0, Top = 0 }
};
Heading h1 = new Heading(1);
h1.Text = “test 1”;
h1.IsAutoSequence = true;
h1.IsInList = true;
h1.TocPage = tocPage;
docPage.Paragraphs.Add(h1);
Heading h11 = new Heading(2);
h11.Text = “test 1.1”;
h11.IsAutoSequence = true;
h11.IsInList = true;
h11.TocPage = tocPage;
docPage.Paragraphs.Add(h11);
Heading h12 = new Heading(2);
h12.Text = “test 1.2”;
h12.IsAutoSequence = true;
h12.IsInList = true;
h12.TocPage = tocPage;
docPage.Paragraphs.Add(h12);
Heading h2 = new Heading(1);
h2.Text = “test 2”;
h2.IsAutoSequence = true;
h2.IsInList = true;
h2.TocPage = tocPage;
docPage.Paragraphs.Add(h2);
tocPage.TocInfo.FormatArrayLength = 5; // assume 5 levels max
for (int index = 0; index < tocPage.TocInfo.FormatArray.Length; index++)
{
LevelFormat lf = tocPage.TocInfo.FormatArray[index];
lf.Margin.Left = (index + 1)*10; // set indent for every level (+ 10 for every level)
}
document.Save(“output.pdf”);
The expected output TOC hierarchy is:
1. test 1
1.1 test 1.1
1.2 test 1.2
2. test 2
Auto-sequence numbers are generated properly but the margin is wrong. For the 1st level it should be 10, for the 2nd - 20 etc. However it seems that the margin that we defined for different level formats are ignored. Moreover, we defined heading for only 2 levels, but the margin for the last record is TOC is 40 and that corresponds to the level 4.
Please let us know what we did wrong and advise how to generate the required output.
**************************************
2. One more issue found. If we just add many headings to a page the “Null Reference” exception is raised on a document save:
for (int i = 0; i < 100; i++)
{
Heading h1 = new Heading(1)
{
Text = "test " + i.ToString(),
IsAutoSequence = true,
IsInList = true,
TocPage = tocPage
};
docPage.Paragraphs.Add(h1);
}
Our document may contain more than 100 heading. Also when we did the same using your “Generator” API everything worked as expected without exceptions.
Please comment.