Table of Contents \h switch issue


I’m running into an issue with the Table of Contents field, and updating it via the UpdateFields method.

We create a number of smaller documents that are then compiled into one comprehensive document. The TC fields are created simply and can contain formatting including bolded text, which I correctly inserted into the TC entry via code like this:

            string tocEntry = string.Format("TC \"{0}\" \\l " + level, entry);

            Field field = builder.InsertField(FieldType.FieldTOCEntry, false);

            //remove the run curently in the field

            //move to the field after (which should put the cursor before the field end node)

            //insert the Toc as Html
            builder.InsertHtml(tocEntry, true);

Which allows me to put Html into the TC fields (a feature that would be useful in the future)

The TOC that we use for them has \f \h \z flags on it, and that’s it. It’s created like this:

        Field toc = builder.InsertTableOfContents("\\f \\z \\h");

        //update the ToC fields

which causes the issue. Without the \h flag there is no issue, and the ToC generates with formatting. When adding the flag, the formatting disappears.

I did manage to find a work around. By inserting the ToC with just the \f and \z flags, calling update fields, and then iterating over every paragraph in the ToC and expanding the PAGEREF field that surrounds the number to surround the entire line instead, I get an identical effect. And then, in order to allow for the document to update the field in the correct way, I track the ToC start and manually add in the \h flag to the run that’s the field code, making sure not to call update fields before I save.

            //expand all page references to the entire line in the ToC
            NodeCollection tocParagraphs = tocSection.Body.GetChildNodes(NodeType.Paragraph, false);
            Run tocRun = null;
            for (int i = 0; i < tocParagraphs.Count; i++)
                var para = (Paragraph) tocParagraphs[i];
                List<Node> childNodeList = para.ChildNodes.Cast<Node>().ToList();
                if (childNodeList.Any(p => p.NodeType == NodeType.FieldStart))
                    Node firstEntryRun = childNodeList[0];
                    //if this is the first paragraph, it will also include the beginning of the 
                    if (firstEntryRun.NodeType == NodeType.FieldStart && ((FieldStart) firstEntryRun).FieldType == FieldType.FieldTOC)
                        tocRun = (Run) childNodeList[1];
                        firstEntryRun = childNodeList[3];

                    Node pageRefFieldStart = childNodeList
                        .First(p => p.NodeType == NodeType.FieldStart && ((FieldStart) p).FieldType == FieldType.FieldPageRef);

                    var pageRefFields = new List<Node>()
                    pageRefFields.ForEach(p =>
                        p.ParentNode.InsertBefore(p, firstEntryRun);

            if (tocRun != null)
                tocRun.Text = tocRun.Text + " \\h";



Thanks for your inquiry. We suggest you please read following article:
Entry Marking Switches

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.



Thank you for the link to the article, I have read it many times, and it was in fact what led me to discover that the \h flag was not working, as that’s where I found out that we could make the entire document line into hyperlinks.

As such, we do not have any input documents, our documents are dynamically generated, but I can provide you with the output and expected output.

Testing (1006.4 KB)

and you should be able to duplicate the issue via this code:

var doc = new Document("c:\\TEMP\\Actual Output.docx");

To get the Expected Output file, all I had to do was open the document and update the table of contents field.

Thank you!



Thanks for your inquiry. The table of contents in both documents are same. Please check the attached images. Actual output.png (28.3 KB)
expected output.png (30.1 KB)
Could you please share the screenshots of problematic section in output document?