Hello, consider following sample code,
Document doc=new Document();
Page _tocPage = doc.Pages.Add();
_tocPage.TocInfo = new TocInfo
{
FormatArrayLength = 3
};
for (int i = 0; i < _tocPage.TocInfo.FormatArray.Length; i++)
{
LevelFormat lf = _tocPage.TocInfo.FormatArray[i];
lf.TextState.FontSize = 10;
lf.Margin.Top = 5;
lf.Margin.Left = (i + 1) * 10; // set indent for every level (+ 10 for every level)
lf.TextState.ForegroundColor = Color.Blue;
if (i == 0)
{
lf.LineDash = TabLeaderType.None;
lf.TextState.FontStyle = FontStyles.Bold;
lf.Margin.Top = 10;
}
if (i == 2)
{
lf.Margin.Left += 10;
}
}
doc.Pages.Add();
for (int i = 0; i < 50; i++)
{
Heading heading = new Heading(1)
{
Text = "Hello"+i
};
doc.Pages[2].Paragraphs.Add(heading);
}
doc.ProcessParagraphs();
for (int i = 0; i < 50; i++)
{
TextFragmentAbsorber absorber = new TextFragmentAbsorber("Hello" + i);
doc.Pages.Accept(absorber);
if (absorber.TextFragments.Count > 0)
{
Heading heading2 = new Heading(1) ;
heading2.IsAutoSequence = true;
heading2.TocPage = doc.Pages[1];
heading2.Text = absorber.TextFragments[1].Text;
heading2.DestinationPage = absorber.TextFragments[1].Page;
heading2.Top = absorber.TextFragments[1].Page.Rect.Height;
doc.Pages[1].Paragraphs.Add(heading2);
}
}
doc.Save(@"H:\userLable.pdf");
While saving the document, following exception is thrown
Invalid index: index should be in the range [1…n] where n equals to the text fragments count.
Now if i put 20 in for loops instead of 50 the code works.
Thank you