.NET Word - Adding Footnotes

Evaluating ASPOSE Word for .NET. Using the following code to add a footnote.

private Table SetTableHeader()
{
    var table = new Table(_document);
    var row = new Row(_document);
    row.RowFormat.AllowBreakAcrossPages = false;
    row.RowFormat.HeadingFormat = true; //Repeat on subsequent Pages
    var cell = new Cell(_document);
    var cellparagraph = new Paragraph(_document);
    cell.AppendChild(cellparagraph);
    cell.FirstParagraph.AppendChild(new Run(_document, _headerName));
    row.AppendChild(cell);
    table.AppendChild(row);
    _document.FirstSection.Body.AppendChild(table);
    AddFootNote(cell.FirstParagraph, footnotes());
    _document.FirstSection.Body.AppendChild(new Paragraph(_document));
    return table;
}

private void AddFootNote(CompositeNode cNode, IEnumerable footnotes)
{
    foreach (var fnote in footnotes)
    {
        var footnote = new Footnote(_document, FootnoteType.Footnote);
        footnote.Paragraphs.Add(new Paragraph(_document));
        footnote.FirstParagraph.Runs.Add(new Run(_document, fnote.FootNoteText));
        footnote.Font.Superscript = true;
        cNode.AppendChild(footnote);
    }
}

The issue is that the Footnotes at the bottom of the page are in plain text and are missing the number. What am I missing? Please advise.

Thanks

Hi there,

Thanks for your inquiry. There is a special character (char)0x2 used inside footnote which represents a placeholder where MS Word will display the footnote number. Please check the following highlighted code snippet. Hope this helps you.

You may use DocumentBuilder.InsertFootnote method insert a footnote or endnote into the document.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Some text is added.");
Footnote footnote = new Footnote(doc, FootnoteType.Footnote);
builder.CurrentParagraph.AppendChild(footnote);
footnote.Paragraphs.Add(new Paragraph(doc));
Run run = new Run(doc, "" + (char)0x2);
run.Font.Position = 5;
run.Font.Size = 6;
footnote.FirstParagraph.Runs.Add(run);
run = new Run(doc, "Footnote text.");
run.Font.Size = 8;
footnote.FirstParagraph.Runs.Add(run);
footnote.Font.Size = 8;
footnote.Font.Superscript = true;
doc.Save(MyDir + "Out.docx");

That appears to have done it.
Thanks Tahir. Did not know about the special character.

Now, how does one go about creating a Reference to the same Footnote? I have tried some of the example from the Form but none have worked.

Thanks again.

Hi there,

Thanks for your inquiry. The second parameter of Footnote constructor is footnoteType. Please change it to Endnote as shown below.

Footnote footnote = new Footnote(doc, FootnoteType.Endnote);

As suggest in my previous post, you may use DocumentBuilder.InsertFootnote method insert a footnote or endnote into the document. Please move the cursor to the desired location and use this method to insert footnote or endnote. Hope this helps you.

Hi,
A bit of confusion, what I need to do is insert a reference to an existing Footnote.
Thanks for your help.

Hi there,

Thanks for your inquiry. Could you please share your input and expected output documents here for our reference? We will then provide you more information about you query along with code.

The attached document shows what I am trying to accomplish.

Thanks again.

Hi there,

Thanks for sharing the detail. You just need to move the cursor to your desired location and insert the footnote. Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + "Sampe+Footnote.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToSection(0);
builder.MoveToParagraph(5, -1);
builder.InsertFootnote(FootnoteType.Footnote, "Footnote text.");
doc.Save(MyDir + "17.3.0.docx");

Thanks Tahir for your patience. Let me further explain my scenario using the sample document I attached earlier. I have a list of custom objects that I iterate through and build a Word Document. The objects in the List have the following graph:

-Header

—Sub header

------Data (has details)

Any one of these Items can have a Footnote object and may point to the same Footnote object. While I am building the document using the Aspose Document Object Model (not the DocumentBuilder) I need to create a Word Footnote when the item has a Footnote object. Now I cannot simply create a new Word Footnote from the Footnote object since I may have previously created a Word Footnote for that Footnote object and if I do, then this will display the same description at the bottom of the page as in

1 Foot note data 1

2 Foot note data 1

Not what is desired. Instead I need to determine if the Word Footnote already exists on the Page and if not create and insert a new Word Footnote otherwise insert a reference to the existing Word Footnote. So the resulting layout is

-Header(1)

—Sub Header(2)

------Data(1)

1 Foot note data 1

2 Foot note data 2

Where Header and Data are both referencing “Foot note data 1”.

Appreciate all the help.

Hi there,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does.

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

Please see attached.

Hi there,

Thanks for sharing the document. In your case, you need to insert the FieldNoteRef field for the footnote. This field inserts the mark of the footnote or endnote that is marked by the specified bookmark. Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + "Sampe Footnote.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get the footnote for which you want to insert the reference. 
Footnote footnote = (Footnote)doc.GetChild(NodeType.Footnote, 0, true);
Bookmark bookmark = ((BookmarkStart)footnote.PreviousSibling).Bookmark; 
// Get the paragraph to which you want to insert a reference 
Paragraph para = (Paragraph)doc.GetChild(NodeType.Paragraph, 10, true);
builder.MoveTo(para);
// Insert the FieldNoteRef 
FieldNoteRef Reffield = (FieldNoteRef)builder.InsertField(FieldType.FieldNoteRef, false);
Reffield.BookmarkName = bookmark.Name;
Reffield.InsertReferenceMark = true;
Reffield.InsertHyperlink = true;
Reffield.Update();
doc.Save(MyDir + "output.docx");