I have used below code to create a bookmark and insert hyper link at end of paragraph . But the hyperlink isnt aligning well with paragraph headings(inserted image) . How to align it properly?
// 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);
}
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";
//get index of current paragraph and insert Back to TOC Hyperlink before the heading
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");
builder.InsertHyperlink("Back to TOC", tokBkName, true);
}