ComponentOne Word for UWP
Working with Word for UWP / Advanced Level Working / Adding TOC
In This Topic
    Adding TOC
    In This Topic

    Word for UWP enables you to add TOC to a word document containing text with the respective headers. These headers can then be used to create a TOC.

    Note that a class named WordUtils is used in the code given below. It is available in the product sample located at the following location on your system:
    Documents\ComponentOne Samples\UWP\WordSample
    You can use these classes in your application from the mentioned location.

    The following code creates a TOC for a word document with text and headers in it:

    Dim _doc As New C1WordDocument()
    
    ' add title
    Dim titleFont As New Font("Tahoma", 24, RtfFontStyle.Bold)
    Dim rcPage As Rect = WordUtils.PageRectangle(doc)
    Dim rc As Rect = WordUtils.RenderParagraph(doc, doc.Info.Title, titleFont, rcPage, rcPage, False)
    rc.Y += 12
    
    ' create nonsense document
    Dim bkmk = New List()
    Dim headerFont As New Font("Arial", 14, RtfFontStyle.Bold)
    Dim bodyFont As New Font("Times New Roman", 11)
    For i As Integer = 0 To 29
            ' create ith header (as a link target and outline entry)
            Dim header As String = String.Format("{0}. {1}", i + 1, BuildRandomTitle())
            rc = WordUtils.RenderParagraph(doc, header, headerFont, rcPage, rc, True, _
                    True)
    
            ' save bookmark to build TOC later
            Dim pageNumber As Integer = doc.CurrentPage() + 1
            bkmk.Add(New String() {pageNumber.ToString(), header})
    
            ' create some text
            rc.X += 36
            rc.Width -= 36
            For j As Integer = 0 To 3 + (_rnd.[Next](20) - 1)
                    Dim text As String = BuildRandomParagraph()
                    rc = WordUtils.RenderParagraph(doc, text, bodyFont, rcPage, rc)
                    rc.Y += 6
            Next
            rc.X -= 36
            rc.Width += 36
            rc.Y += 20
    Next
    
    ' start Table of Contents
    doc.PageBreak()
    ' start TOC on a new page
    Dim tocPage As Integer = doc.CurrentPage()
    ' save page index (to move TOC later)
    rc = WordUtils.RenderParagraph(doc, Strings.TableOfContentsDocumentTitle, titleFont, rcPage, rcPage, True)
    rc.Y += 12
    rc.X += 30
    rc.Width -= 40
    
    ' render Table of Contents
    Dim dottedPen As New Pen(Colors.Gray, 1.5F)
    dottedPen.DashStyle = DashStyle.Dot
    Dim sfRight As New StringFormat()
    sfRight.Alignment = HorizontalAlignment.Right
    rc.Height = bodyFont.Size * 1.2
    For Each entry As String() In bkmk
            ' get bookmark info
            Dim page As String = entry(0)
            Dim header As String = entry(1)
    
            ' render header name and page number
            doc.DrawString(header, bodyFont, Colors.Black, rc)
            doc.DrawString(page, bodyFont, Colors.Black, rc, sfRight)
    
            ' add local hyperlink to entry
            doc.AddLink(Strings.PoundSign + header)
    
            ' move on to next entry
            rc = WordUtils.Offset(rc, 0, rc.Height)
            If rc.Bottom > rcPage.Bottom Then
                    doc.PageBreak()
                    rc.Y = rcPage.Y
            End If
    Next
    
    C1WordDocument _doc = new C1WordDocument();
    
    // add title
    Font titleFont = new Font("Tahoma", 24, RtfFontStyle.Bold);
    Rect rcPage = WordUtils.PageRectangle(doc);
    Rect rc = WordUtils.RenderParagraph(doc, doc.Info.Title, titleFont, rcPage, rcPage, false);
    rc.Y += 12;
    
    // create nonsense document
    var bkmk = new List();
    Font headerFont = new Font("Arial", 14, RtfFontStyle.Bold);
    Font bodyFont = new Font("Times New Roman", 11);
    for (int i = 0; i < 30; i++) {
      // create ith header (as a link target and outline entry)
      string header = string.Format("{0}. {1}", i + 1, BuildRandomTitle());
      rc = WordUtils.RenderParagraph(doc, header, headerFont, rcPage, rc, true, true);
    
      // save bookmark to build TOC later
      int pageNumber = doc.CurrentPage() + 1;
      bkmk.Add(new string[] {
        pageNumber.ToString(), header
      });
    
      // create some text
      rc.X += 36;
      rc.Width -= 36;
      for (int j = 0; j < 3 + _rnd.Next(20); j++) {
        string text = BuildRandomParagraph();
        rc = WordUtils.RenderParagraph(doc, text, bodyFont, rcPage, rc);
        rc.Y += 6;
      }
      rc.X -= 36;
      rc.Width += 36;
      rc.Y += 20;
    }
    
    // start Table of Contents
    doc.PageBreak(); // start TOC on a new page
    int tocPage = doc.CurrentPage(); // save page index (to move TOC later)
    rc = WordUtils.RenderParagraph(doc, Strings.TableOfContentsDocumentTitle, titleFont, rcPage, rcPage, true);
    rc.Y += 12;
    rc.X += 30;
    rc.Width -= 40;
    
    // render Table of Contents
    Pen dottedPen = new Pen(Colors.Gray, 1.5 f);
    dottedPen.DashStyle = DashStyle.Dot;
    StringFormat sfRight = new StringFormat();
    sfRight.Alignment = HorizontalAlignment.Right;
    rc.Height = bodyFont.Size * 1.2;
    foreach(string[] entry in bkmk) {
      // get bookmark info
      string page = entry[0];
      string header = entry[1];
    
      // render header name and page number
      doc.DrawString(header, bodyFont, Colors.Black, rc);
      doc.DrawString(page, bodyFont, Colors.Black, rc, sfRight);
    
      // add local hyperlink to entry
      doc.AddLink(Strings.PoundSign + header);
    
      // move on to next entry
      rc = WordUtils.Offset(rc, 0, rc.Height);
      if (rc.Bottom > rcPage.Bottom) {
        doc.PageBreak();
        rc.Y = rcPage.Y;
      }
    }
    

    The above code adds bookmark to the headers of the text in document. These bookmarks are then used to generate a TOC.

    The output of the above code will look similar to the image given below: