@alexey.noskov I am trying to add the toc to particular heading 1 below, I used below mention the code but the below code is adding the TOC to above the particular heading kindly help me.
Please find the below mentioned the input and expected output document.
Heading 1.docx (14.6 KB)
Expected_output_Heading.docx (17.1 KB)
string heading = "TABLE OF CONTENTS";
var paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>();
foreach (Paragraph p in paragraphs)
{
if (p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1 &&
p.ToString(SaveFormat.Text).Trim().Equals(heading, StringComparison.InvariantCultureIgnoreCase))
{
var tocFields = doc.Range.Fields.Where(f => f.Type == FieldType.FieldTOC);
if (!tocFields.Any())
{
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
doc.Styles[StyleIdentifier.Toc1].Font.Color = Color.Blue;
}
doc.UpdateFields();
}
}