Back to TOC shortcut is taking cursor to Ending of TOC instead of Starting of TOC. Please help

I have used below code to insert Back to TOC shortcut after every paragraph. I want to insert at end of every paragraph a shortcut which will take the users to starting of TOC. But instead it is taking to ending of TOC. please help. Also the Back to TOC isnt being aligned with Headings. I have attached my code and the document.

foreach (Paragraph paragraph in doc.GetChildNodes(NodeType.Paragraph, true))
{
    int index;

    if (paragraph.GetAncestor(NodeType.Table) != null)
        continue;

    string paraText = paragraph.ToString(SaveFormat.Text).Trim();

    // Check if the paragraph text matches the section pattern
    if (Regex.IsMatch(paraText, @"^\d+(\.\d{1})+\s*"))
    {
        // Apply the desired style to the paragraph without numbering
        paragraph.ParagraphFormat.StyleName = "Heading 11";

        index = paragraph.Document.GetChildNodes(NodeType.Paragraph, true).IndexOf(paragraph);

        builder.MoveToParagraph(index - 1, -1);

        builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
        builder.InsertField("HYPERLINK \"#TOC\" \\l \"TOC\"", "Back to TOC");
    }

1273-02-0042-001.docx (70.8 KB)

@hrnallap

To ensure that the “Back to TOC” hyperlink takes users to the beginning of the Table of Contents (TOC) instead of the end, you need to make sure that the bookmark for the TOC is correctly set at the start of the TOC section.

Here’s how you can modify your code to achieve this:

  1. Set a Bookmark at the Start of the TOC: Before inserting the hyperlink, ensure that you have a bookmark at the beginning of the TOC. You can do this by adding a bookmark in your document where the TOC starts.

  2. Modify the Hyperlink Field: Change the hyperlink field to point to the bookmark you created instead of using the default TOC reference.

Here’s an example of how you can implement this:

// Assuming you have already created a bookmark at the start of the TOC
string tocBookmarkName = "StartOfTOC"; // Name of the bookmark at the start of the TOC

foreach (Paragraph paragraph in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (paragraph.GetAncestor(NodeType.Table) != null)
        continue;

    string paraText = paragraph.ToString(SaveFormat.Text).Trim();

    // Check if the paragraph text matches the section pattern
    if (Regex.IsMatch(paraText, @"^\d+(\.\d{1})+\s*"))
    {
        // Apply the desired style to the paragraph without numbering
        paragraph.ParagraphFormat.StyleName = "Heading 11";

        int index = paragraph.Document.GetChildNodes(NodeType.Paragraph, true).IndexOf(paragraph);
        
        builder.MoveToParagraph(index, -1); // Move to the current paragraph

        builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
        // Change the hyperlink to point to the bookmark
        builder.InsertField($"HYPERLINK \"#{tocBookmarkName}\" \\l \"TOC\"", "Back to TOC");
    }
}

Alignment with Headings

To ensure that the “Back to TOC” hyperlink aligns properly with the headings, you may need to adjust the paragraph formatting. You can set the alignment and indentation properties of the paragraph where the hyperlink is inserted to match the headings.

Summary

  • Create a bookmark at the start of the TOC.
  • Modify the hyperlink to point to this bookmark.
  • Adjust the paragraph formatting to align the hyperlink with the headings.

This should resolve the issue of the hyperlink taking users to the end of the TOC and ensure proper alignment with the headings. If you have any further questions or need additional assistance, feel free to ask!

@hrnallap You can use the following code to achieve this:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert bookmark at the begging of TOC.
FieldToc toc = (FieldToc)doc.Range.Fields.Where(f => f.Type == FieldType.FieldTOC).FirstOrDefault();
string tokBkName = "TOC_Bookmark";
if (toc != null)
{
    builder.MoveToField(toc, false);
    builder.StartBookmark(tokBkName);
    builder.EndBookmark(tokBkName);

    // Add "Back to TOC" hyperlinks.
    foreach (Paragraph paragraph in doc.GetChildNodes(NodeType.Paragraph, true))
    {
        if (paragraph.GetAncestor(NodeType.Table) != null)
            continue;

        if (paragraph.ParagraphFormat.IsHeading)
        {
            builder.MoveTo(paragraph);

            builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
            builder.InsertHyperlink("Back to TOC", tokBkName, true);
        }
    }
}

doc.Save(@"C:\Temp\out.docx");