Hi Team,
I am trying to add the Table of content in particular heading below . I used below code its adding TOC but its not moving the heading below the content to next line. image.png (3.9 KB)
Please find the below input and expected output file.
Expected_output (3).docx (165.0 KB)
Input_document_Table of content.docx (163.2 KB)
bool hasToc = doc.Range.Fields.Where(f => f.Type ==
FieldType.FieldTOC).Any();
if (!hasToc)
{
string headingToFind = "TABLE OF CONTENTS";
var target = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>();
foreach (Paragraph p in target)
{
if (p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
{
var isHeading = "";
foreach (Run runHeading in p.Runs)
{
isHeading = isHeading + runHeading.Text;
}
if (isHeading.Trim().Equals(headingToFind, StringComparison.InvariantCultureIgnoreCase))
{
if (p != null)
{
DocumentBuilder builder = new DocumentBuilder(ruleBaseModel.SourceDocument);
builder.MoveTo(p.NextSibling);
// builder.Writeln();
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
ruleBaseModel.SourceDocument.Styles[StyleIdentifier.Toc1].Font.Color = Color.Blue;
}
ruleBaseModel.SourceDocument.UpdateFields();
}
break;
}
}
}