We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Replace Page Break after TOC in Word Document with Section Break (C# .NET)

There is a page break in my word document after TOC.
I need code to move the document builder to end of toc and check there is a page break between toc and content, then replace the page break with section break.

Hope you solve my problems as early as possible.

There is page break after table of contents in my word doc, i need to change the page break into section break… could you please tell me how to do this.

@HarishGali

Following code example shows how to replace page break with section break after TOC field. Hope this helps you.

Document doc = new Document(MyDir + @"input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
FieldToc fieldToc = (FieldToc)doc.Range.Fields.Cast<Field>().Where(f => f.Type == FieldType.FieldTOC).ToList().FirstOrDefault<Field>();

if (fieldToc != null)
{
    Node node = fieldToc.End.ParentParagraph;
    while (node != null)
    {
        node = node.NextPreOrder(doc);
        if (node.GetText().Contains(ControlChar.PageBreak))
        {
            builder.MoveTo(node);
            builder.InsertBreak(BreakType.SectionBreakNewPage);
            node.Remove();
            break;
        }
                           
    }
}
               
doc.UpdateFields();
doc.Save(MyDir + "21.11.docx");