Formatting a table of contents

I have a dynamically created document and am inserting a Table of Contents. However when displayed it is retaining the formatting which is applied to each heading at build time. I have tried to overwrite this as shown below. Is there any other way to apply formatting to a toc?

builder.Font.Name = "Arial"
builder.Font.Size = 10
builder.InsertTableOfContents("\o ""1-4"" \h \z \u")
builder.Font.ClearFormatting()

Hi
Thanks for your request. Could you please attach your document and code that will allow us to reproduce the problem? We will check the issue and provide you more information.
Best regards,

Thanks for your help in this.
I have attached an example as requested.
This also highlights another problem I had (can’t remember how I got round it). The TOC appears on different pages for each heading.

Hi
Thank you for additional information. The problem occurs because column and page breaks do not force a paragraph break that is why when you set heading style, this style is applied to the whole paragraph and you see in the TOC unwanted content. Please see the following modified code that produces the correct output:

Public Sub CreateReport()
    Dim newdoc As New Document()
    newdoc.RemoveAllChildren()
    Dim dbr As New DocumentBuilder(newdoc)
    With dbr.PageSetup
        .PaperSize = PaperSize.A4
        .Orientation = Orientation.Portrait
        .TopMargin = ConvertUtil.MillimeterToPoint(20)
        .BottomMargin = ConvertUtil.MillimeterToPoint(20)
        .LeftMargin = ConvertUtil.MillimeterToPoint(25)
        .RightMargin = ConvertUtil.MillimeterToPoint(25)
        mxHeaderFooterWidth = .PageWidth - .LeftMargin - .RightMargin
    End With
    dbr.CurrentSection.PageSetup.DifferentFirstPageHeaderFooter = True
    ' --- Insert Table of Contents ---
    BuildToc(newdoc, dbr)
    dbr.InsertBreak(BreakType.SectionBreakNewPage)
    dbr.CurrentSection.PageSetup.TextColumns.Spacing = ConvertUtil.MillimeterToPoint(10)
    dbr.CurrentSection.PageSetup.TextColumns.SetCount(2)
    ' --- Insert Content ---
    dbr.Font.Name = "ITC Stone Sans Medium"
    dbr.Font.Size = 16
    dbr.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1
    dbr.Writeln("Title 1")
    dbr.ParagraphFormat.ClearFormatting()
    dbr.Writeln(vbCrLf)
    dbr.Font.Name = "Arial"
    dbr.Font.Size = 10
    dbr.Writeln("content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1")
    dbr.Font.ClearFormatting()
    dbr.InsertBreak(BreakType.ColumnBreak)
    dbr.Font.Name = "ITC Franklin Gothic Demi"
    dbr.Font.Size = 12
    dbr.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2
    dbr.Writeln("Title 2")
    dbr.ParagraphFormat.ClearFormatting()
    dbr.Writeln(vbCrLf)
    dbr.Font.Name = "Arial"
    dbr.Font.Size = 10
    dbr.Writeln("content 2 content 2 content 2 content 2 content 2content 2content 2content 2 content 2 content 2 content 2 content 2 content 2 content 2 content 2content 2")
    dbr.Font.ClearFormatting()
    dbr.InsertBreak(BreakType.ColumnBreak)
    dbr.Font.Name = "ITC Franklin Gothic Book"
    dbr.Font.Size = 10
    dbr.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3
    dbr.Writeln("Title 3")
    dbr.ParagraphFormat.ClearFormatting()
    dbr.Writeln(vbCrLf)
    dbr.Font.Name = "Arial"
    dbr.Font.Size = 10
    dbr.Writeln("content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3")
    dbr.Font.ClearFormatting()
    newdoc.UpdateFields()
    SaveOpenDoc(newdoc)
End Sub

Hope this helps.
Best regards,

Thanks, this does remove ‘Content1…’ etc from the toc but it is still split up over three pages and retains the StyleIdentifier applied to each heading instead of the new formatting style being applied ie Arial font size 10

Hi Keith,
Thank you for additional information. But could you please also attach the expected output? I will check it and provide you more information.
Best regards,

Thanks
Document of expected output attached as requested.

Hi
Thanks for your request. You can change formatting of TOC items by changing Toc1, Toc2…Toc9 styles. Also, in your code you explicitly specify formatting of text in headings. I think it would be better to set this formatting in styles. Please see the following code:

Public Sub CreateReport()
    Dim newdoc As New Document()
    newdoc.RemoveAllChildren()
    Dim dbr As New DocumentBuilder(newdoc)
    With dbr.PageSetup
        .PaperSize = PaperSize.A4
        .Orientation = Orientation.Portrait
        .TopMargin = ConvertUtil.MillimeterToPoint(20)
        .BottomMargin = ConvertUtil.MillimeterToPoint(20)
        .LeftMargin = ConvertUtil.MillimeterToPoint(25)
        .RightMargin = ConvertUtil.MillimeterToPoint(25)
        mxHeaderFooterWidth = .PageWidth - .LeftMargin - .RightMargin
    End With
    dbr.CurrentSection.PageSetup.DifferentFirstPageHeaderFooter = True
    ' --- Setup styles of headings and toc items ---
    SetupStyles(newdoc)
    ' --- Insert Table of Contents ---
    BuildToc(newdoc, dbr)
    dbr.InsertBreak(BreakType.SectionBreakNewPage)
    dbr.CurrentSection.PageSetup.TextColumns.Spacing = ConvertUtil.MillimeterToPoint(10)
    dbr.CurrentSection.PageSetup.TextColumns.SetCount(2)
    ' --- Insert Content ---
    dbr.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1
    dbr.Writeln("Title 1")
    dbr.ParagraphFormat.ClearFormatting()
    dbr.Font.Name = "Arial"
    dbr.Font.Size = 10
    dbr.Writeln("content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1 content 1")
    dbr.Font.ClearFormatting()
    dbr.InsertBreak(BreakType.ColumnBreak)
    dbr.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2
    dbr.Writeln("Title 2")
    dbr.ParagraphFormat.ClearFormatting()
    dbr.Font.Name = "Arial"
    dbr.Font.Size = 10
    dbr.Writeln("content 2 content 2 content 2 content 2 content 2content 2content 2content 2 content 2 content 2 content 2 content 2 content 2 content 2 content 2content 2")
    dbr.Font.ClearFormatting()
    dbr.InsertBreak(BreakType.ColumnBreak)
    dbr.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3
    dbr.Writeln("Title 3")
    dbr.ParagraphFormat.ClearFormatting()
    dbr.Font.Name = "Arial"
    dbr.Font.Size = 10
    dbr.Writeln("content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3 content 3")
    dbr.Font.ClearFormatting()
    newdoc.UpdateFields()
    SaveOpenDoc(newdoc)
End Sub
Public Sub BuildToc(ByVal newdoc As Document, ByVal dbr As DocumentBuilder)
    With dbr
        .StartBookmark("toc")
        .Font.Name = "Arial"
        .Font.Size = 12
        .Writeln("Contents")
        .InsertTableOfContents("\o ""1-4"" \h \z \u")
        .EndBookmark("toc")
    End With
End Sub
Private Sub SetupStyles(ByVal doc As Document)
    ' To change formatting of toc items you should change Toc1, Toc2...Toc9 styles.
    Dim toc1 As Style = doc.Styles(StyleIdentifier.Toc1)
    Dim toc2 As Style = doc.Styles(StyleIdentifier.Toc2)
    Dim toc3 As Style = doc.Styles(StyleIdentifier.Toc3)
    With toc1
        .Font.Name = "Arial"
        .Font.Size = 10
    End With
    With toc2
        .Font.Name = "Arial"
        .Font.Size = 10
    End With
    With toc3
        .Font.Name = "Arial"
        .Font.Size = 10
    End With
    ' Setup heading styles.
    Dim h1 As Style = doc.Styles(StyleIdentifier.Heading1)
    Dim h2 As Style = doc.Styles(StyleIdentifier.Heading2)
    Dim h3 As Style = doc.Styles(StyleIdentifier.Heading3)
    With h1
        .Font.Name = "ITC Stone Sans Medium"
        .Font.Size = 16
    End With
    With h2
        .Font.Name = "ITC Stone Sans Medium"
        .Font.Size = 12
    End With
    With h3
        .Font.Name = "ITC Stone Sans Medium"
        .Font.Size = 10
    End With
End Sub

Hope it will help you to achieve what you need.
Best regards,

That’s great, font now set for TOC independently. The TOC items are however still appearing on seperate pages.
Many Thanks

Hi
Thank you for additional information. Do you mean “Title 3” appears on the next page? If so, this is expected. You can see the same in the document you attached as expected result. Please clarify.
Best regards,

Hi
No the TOC is split up over three pages.
Please see example attached.

Hi
Thank you for additional information. But I cannot reproduce the problem on my side. I used the latest version of Aspose.Words for testing. You can download the latest version from here:
https://releases.aspose.com/words/net
As I can see you are using 9.2.0 version for testing. Please try using the latest version.
Best regards,