Use Bookmarks to create Table of Contents

I am able to create a Word doc with Bookmarks. But I can’t get the Table of Contents to create based off of the Bookmarks.

Here’s my code to create the TOC. It works when I use the styling options, but I can’t change the styles, and the files are uploaded so I can’t know what they’ve done with styles.

docBuilder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u \\b \"Test\" ");

The TOC finds the bookmark, because it errors when it does not, and now it says “no table of contents entries found”. What am I missing?

Bookmarks.zip (19.5 KB)

@jsierks,

Please also ZIP and attach your expected document showing the correct output here for our reference. You can create expected document by using MS Word. Please also list the complete steps that you performed in MS Word to create the expected document on your end. We will then start investigation into your scenario and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

The expected document is the one I attached but with a Table of Contents. Here’s a PDF version that shows the TOC (create another way of course)
Bookmarks2.zip (69.1 KB)
.

There were no steps in Word. The bookmarks and TOC are all being created in code - C# .NET.

And I feel your reply came without actually reading the problem. So I will reiterate it here. The attached document has Bookmarks. I want to use those Bookmarks to create a Table of Contents. But I haven’t been able to. And I can’t find any current documentation on your site about it - only old forum posts, which I’ve cobbled together to get this far. (You may notice in the previously attached Word document that I have a bunch of extra Bookmarks; those aren’t needed, but were added to try to get this working.)

Here’s the Word doc with the TOC.
Bookmarks3.zip (10.4 KB)

@jsierks,

I am afraid, there is no TOC field inside Bookmarks3.docx document. When using the TOC field, it does not seem possible to get the desired output even when using MS Word. Are you able to get the desired Table of Contents output by using the TOC field in MS Word?

Please refer to the following article to learn how does the \b switch of TOC field work?
Build a partial table of content based on bookmark (see under A partial table of contents)

However, if you would like to manually parse all bookmarks in Word document and then create a Table of Contents like content, please see the following draft code:

Document doc = new Document("E:\\temp\\Bookmarks\\Bookmarks.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToDocumentEnd();
builder.Writeln();

LayoutCollector collector = new LayoutCollector(doc);
foreach (Bookmark bm in doc.Range.Bookmarks)
{
    Paragraph para = (Paragraph)bm.BookmarkStart.GetAncestor(NodeType.Paragraph);

    if (para.Runs.Count > 0 && !string.IsNullOrEmpty(para.ToString(SaveFormat.Text).Trim()))
    {
        builder.ParagraphFormat.TabStops.Add(72 * 6, TabAlignment.Right, TabLeader.None);
        int pageNumber = collector.GetStartPageIndex(para.Runs[0]);
        builder.InsertHyperlink(para.ToString(SaveFormat.Text).Trim(), bm.Name, true);
        builder.Write(ControlChar.Tab + pageNumber + ControlChar.ParagraphBreak);
    }
}

doc.Save("E:\\Temp\\Bookmarks\\19.11.docx");

Hope, this helps in achieving what you are looking for.

Thank you, that link was very helpful. It looks like “\b” still looks at styles within a bookmark range. I was hoping/thinking it used all the bookmarks within that range.

So I will have to build it via inserting the TC field, as explained elsewhere on your site.

@jsierks,

Thanks for your feedback. It is great that you were able to find what you were looking for. Please let us know any time you have any further queries in future.