Table Content is missing

Please find attached document. PAP_RS43733_GLB202302016_20230327150453.docx (85.2 KB). I already added builder.InsertTableOfContents("\o “1-3” \h \z \u"); but still index is not generating.

Below Code:

Stream HeaderTemplate = ePAPS_FileUtility.GetFile(ConfigurationManager.AppSettings["Templates"] + "PAPHeader.docx");
Stream ApprovalTemplate = ePAPS_FileUtility.GetFile(ConfigurationManager.AppSettings["Templates"] + "PAPApprovals.docx");
Aspose.Words.Document headerDoc = AsposeHelper.Document.WordDoc(HeaderTemplate);
DataTable SecData = Data.Tables[2];
engine.BuildReport(headerDoc, report);
Document doc = AsposeHelper.Document.WordDoc(ApprovalTemplate);
engine.BuildReport(doc, report);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("Table");
String strHTML = buildHtml(report);
builder.InsertHtml(strHTML);
String strHTML_Approvers_Comments = BuildHtml_Approvers_Comments(report);
builder.InsertHtml(strHTML_Approvers_Comments);
String strHTML_Taxonomy = buildESPTaxonomyHtml(Request_Number);
builder.InsertHtml(strHTML_Taxonomy);
Aspose.Words.Font font = builder.Font;
builder.MoveToBookmark("TOC");
builder.InsertHtml("<p style='font-weight:normal;font-family:Arial;font-size:24pt;color:#2E74B5;margin-bottom:0px;'>Table of Content</p> <br/> <br/>"); //Added for header
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.MoveToBookmark("Main");
await WriteData(SecData, builder, Request_Number: Request_Number, soeid: soeid);
headerDoc.AppendDocument(RemoveBlankParagraph(doc), Aspose.Words.ImportFormatMode.KeepSourceFormatting);
setTOCStyle(headerDoc, StyleIdentifier.Toc1);
setTOCStyle(headerDoc, StyleIdentifier.Toc2);
setTOCStyle(headerDoc, StyleIdentifier.Toc3);
headerDoc.UpdateFields();
return headerDoc;

@rs43733 in your posted code you are using several unknown methods so I’m not sure about what you are doing. I was able to insert a TOC in your file, first I turn “Table of Content” paragraph as Heading 1, than can be done:

builder.InsertHtml("<h1 style='font-weight:normal;font-family:Arial;font-size:24pt;color:#2E74B5;margin-bottom:0px;'>Table of Content</h1> <br/> <br/>"); //Added for header

After that I ran the following code:

Document doc = new Document("C:\\Temp\\input.docx");
bool hasToc = doc.Range.Fields.Where(f => f.Type == FieldType.FieldTOC).Any();

if (!hasToc)
{
    const string headingToFind = "Table of Content";

    var target = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().FirstOrDefault(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1);
    if (target != null)
    {
        var isHeading = string.Join(string.Empty, target.Runs.Select(r => ((Run)r).Text));
        if (isHeading.Trim().Equals(headingToFind, StringComparison.InvariantCultureIgnoreCase))
        {   
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.MoveTo(target);
            //builder.InsertBreak(BreakType.ParagraphBreak);
            builder.Writeln();
            builder.ParagraphFormat.ClearFormatting();
 
            builder.CurrentParagraph;
            builder.InsertBreak(BreakType.ParagraphBreak);

            var toc = builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
            doc.Styles[StyleIdentifier.Toc1].Font.Color = Color.Blue;
        }
        doc.UpdateFields();
    }
}
doc.Save("C:\\Temp\\output.docx");

As you may have noticed, I am using the same code as you: builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u") . Therefore, there may be something happening in the methods that are being referenced in the code you posted.
output.docx (101.5 KB)

I might don’t have title “Table of Content”. So we can’t compare the string. My requirement is if I don’t have any index on page#2 then need to insert index.

Please find attached document.
PAP_RS43733_GLB202302016_20230328110808.docx (85.2 KB)

@rs43733 you can use the following code to achieve this:

Document doc = new Document("C:\\Temp\\input.docx");
bool hasToc = doc.Range.Fields.Where(f => f.Type == FieldType.FieldTOC).Any();

if (!hasToc)
{
    LayoutCollector collector = new LayoutCollector(doc);
    LayoutEnumerator enumerator = new LayoutEnumerator(doc);

    DocumentBuilder builder = new DocumentBuilder(doc);

    var paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
    foreach ( var paragraph in paragraphs) 
    { 
        var startPage = collector.GetStartPageIndex(paragraph);
        if(startPage == 2)
        {
            builder.MoveTo(paragraph);
            builder.InsertBreak(BreakType.ParagraphBreak);
            builder.InsertHtml("<h1 style='font-weight:normal;font-family:Arial;font-size:24pt;color:#2E74B5;margin-bottom:0px;'>Table of Content</h1>"); //Added for header
            var toc = builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
            builder.InsertBreak(BreakType.LineBreak);

            var p = builder.InsertParagraph();
            var clonPara = paragraph.Clone(true);

            p.ParentNode.InsertAfter(paragraph, p);

            paragraph.Remove();
            p.Remove();
            break;
        }

        var endPage = collector.GetEndPageIndex(paragraph);
        if (endPage == 2)
        {
            builder.MoveTo(paragraph);
            builder.InsertBreak(BreakType.ParagraphBreak);
            builder.InsertHtml("<h1 style='font-weight:normal;font-family:Arial;font-size:24pt;color:#2E74B5;margin-bottom:0px;'>Table of Content</h1>"); //Added for header
            builder.InsertBreak(BreakType.LineBreak);
            builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

            builder.InsertBreak(BreakType.ParagraphBreak);
            break;
        }
    }

    doc.UpdateFields();
}
doc.Save("C:\\Temp\\output.docx");