Finding the Reference to a Footnote in the main Word Document Body

I have a requirement to insert a hyperlink just before the reference to a footnote within the main body of a Document. I can successfully browse through all the Footnotes in code within Aspose.Words and this gives me the Text within the footnote itself but I can’t see how to find the location which is being footnoted.
In Microsoft.Office.Interop.Word I have found this as a Range presented as the Footnote.Reference project which then allowed my to do a oDoc.Hyperlinks.Add(Footnote.Reference, ref address, ref missing, ref missing, ref textToDisplay, ref missing) but I obviously need to do this in Aspose.Words for .Net. Can you please advise how to achieve this in Aspose.Words?

Hi Nigel,

Thanks for your inquiry. The Footnote class is used to represent both footnotes and end notes in a Word document. Footnote is an inline-level node and can only be a child of Paragraph.

Please use the following code snippet to insert hyperlink before footnoted paragraph. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + @"newFootnote.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Footnote footnote in doc.GetChildNodes(NodeType.Footnote, true))
{
    Paragraph para = (Paragraph)footnote.GetAncestor(NodeType.Paragraph);
    builder.MoveTo(para.Runs[0]);
    // Specify font formatting for the hyperlink.
    builder.Font.Color = Color.Blue;
    builder.Font.Underline = Underline.Single;
    builder.InsertHyperlink("Aspose Website", "http://www.aspose.com", false);
    // Revert to default formatting.
    builder.Font.ClearFormatting();
}
doc.Save(MyDir + @"out.docx");

Thanks for the help but I am still having some difficulties. The builder.MoveTo(para.Runs[0]) places my hyperlink at the start of the paragraph which contains my footnote reference instead of just before the reference itself. In addition when I have 2 references in the same paragraph (or even the same word) then I only get 1 hyperlink instead of 2.
For information the user requirement I am fullfulling means I have to put the hyperlink just in front of each reference even if we have 2 references together (i.e. words word words)

Hi Nigel,

Thanks for sharing the details. I have modified the code according to your requirement. Please use the following code snippet to insert hyperlink before footnote reference. Hope this helps you.

If you still face problem, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate that how you want your final Word output be generated like. We will then provide you more information on this along with code.

Document doc = new Document(MyDir + @"newFootnote.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Footnote footnote in doc.GetChildNodes(NodeType.Footnote, true))
{
    // Get the Footnoted Paragraph 
    Paragraph para = (Paragraph)footnote.GetAncestor(NodeType.Paragraph);
    // Get the last run node of Paragraph
    Run lastRun = (Run)footnote.PreviousSibling;
    // Create an empty run before FootNote node
    Run run = new Run(doc, " ");
    lastRun.ParentNode.InsertAfter(run, lastRun);
    builder.MoveTo(run);
    // Specify font formatting for the hyperlink.
    builder.Font.Color = Color.Blue;
    builder.Font.Underline = Underline.Single;
    builder.InsertHyperlink("Aspose Website", "http://www.aspose.com", false);
    // Revert to default formatting.
    builder.Font.ClearFormatting();
}
doc.Save(MyDir + @"out.docx");

Your code worked perfectly. Many Thanks

Hi Nigel,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.