Hi Yogesh,
Try adding this ConvertTocToTable
method after calling toc.Update()
:
static void ConvertTocToTable(TableOfEntries toc)
{
var document = toc.Document;
var table = new Table(document);
table.TableFormat.AutomaticallyResizeToFitContents = false;
table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
table.Rows.Add(new TableRow(document,
new TableCell(document, new Paragraph(document, "Sr No.")) { CellFormat = { PreferredWidth = new TableWidth(15, TableWidthUnit.Percentage) } },
new TableCell(document, new Paragraph(document, "Name")) { CellFormat = { PreferredWidth = new TableWidth(70, TableWidthUnit.Percentage) } },
new TableCell(document, new Paragraph(document, "Page")) { CellFormat = { PreferredWidth = new TableWidth(15, TableWidthUnit.Percentage) } }));
for (int i = 0; i < toc.Entries.Count; i++)
{
var entry = toc.Entries[i] as Paragraph;
var hyperlink = entry.Inlines[0] as Hyperlink;
var page = hyperlink.DisplayInlines[2].Clone(true) as Field;
var name = hyperlink.Clone(true);
name.DisplayInlines.RemoveAt(2); // Remove "PageRef" field.
name.DisplayInlines.RemoveAt(1); // Remove tab.
table.Rows.Add(new TableRow(document,
new TableCell(document, new Paragraph(document, $"{i + 1}")),
new TableCell(document, new Paragraph(document, name)),
new TableCell(document, new Paragraph(document, page))));
}
var content = toc.Content.Set(table.Content);
var index = new Paragraph(document, "Index");
index.ParagraphFormat.Alignment = HorizontalAlignment.Center;
((Run)index.Inlines[0]).CharacterFormat.Size = 20;
content.Start.InsertRange(index.Content);
}
Does this solve your issue?
Regards,
Mario