How to update 'Table of Contents' in a Word document using C# and VB.Net

There is a simple DOCX document with TOC containg two Chapters and SubChapters.

For example: We need to modify or add some text and images inside of these Chapters.

 

We have added images and text. Our Chapters (headlines) are have shifted to other pages. We need to Update our TOC using Document.Net

How to do that:

  1. First of all, load a DOCX file, which need to update TOC.
    
    DocumentCore document = DocumentCore.Load(@"d:\Download\TOC.docx");
    
    ´╗┐DocumentCore is root class, it represents a document itself.
  2. You can automatically update it if you make changes in your document.
    TOC can be updated only after all document content is added.
    
       var toc = (TableOfEntries)document.GetChildElements(true, ElementType.TableOfEntries).FirstOrDefault();
       toc.Update();
    
    // Update TOC's page numbers.
    // Page numbers are automatically updated in that case.
       document.GetPaginator(new PaginatorOptions() { UpdateFields = true });
    
    Gets the document paginator and updates the document based on the specified options. The paginator options used to specify which parts of the document to update while paginating it.
  3. Save our document as DOCX.
    
        // Save DOCX to a file
        document.Save("TOC-Updated.docx");
    
    Download the resulting Docx-file: TOC-Updated.docx
 

Complete code

using System.Linq;
using SautinSoft.Document;

namespace Example
{
    class Sample
    {
        static void Main(string[] args)
        {
            TOC_Update();
        }

        public static void TOC_Update()
        {

            // Let's create a simple document.
            DocumentCore dc = new DocumentCore();

            //It's easy to load any document.
            dc = DocumentCore.Load(@"..\..\toc.docx");

            // Update TOC (TOC can be updated only after all document content is added).
            var toc = (TableOfEntries)dc.GetChildElements(true, ElementType.TableOfEntries).FirstOrDefault();
            toc.Update();

            // Update TOC's page numbers.
            // Page numbers are automatically updated in that case.
            dc.GetPaginator(new PaginatorOptions() { UpdateFields = true });

            // Save DOCX to a file
            dc.Save(@"..\..\TOC_Updated.docx");

            // Open the result for demonstation purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(@"..\..\TOC_Updated.docx") { UseShellExecute = true });
        }
    }
}

Download.

        
            Imports System.Linq
Imports SautinSoft.Document

Module ExampleVB
    Sub Main()
        TOC_Update()
    End Sub

    Public Sub TOC_Update()

        ' Let's create a simple document.
        Dim dc As New DocumentCore()

        'It's easy to load any document.
        dc = DocumentCore.Load("..\toc.docx")

        ' Update TOC (TOC can be updated only after all document content is added).
        Dim toc = CType(dc.GetChildElements(True, ElementType.TableOfEntries).FirstOrDefault(), TableOfEntries)
        toc.Update()

        ' Update TOC's page numbers.
        ' Page numbers are automatically updated in that case.
        dc.GetPaginator(New PaginatorOptions() With {.UpdateFields = True})

        ' Save DOCX to a file
        dc.Save("..\TOC_Updated.docx")

        ' Open the result for demonstation purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("..\TOC_Updated.docx") With {.UseShellExecute = True})
    End Sub
End Module
          

Download.

© SautinSoft 2019