Search Results for

    Show / Hide Table of Contents

    Footnotes

    Version 6.1.0

    In this article

    • Implementing footnotes
    • Adding a footnote reference
    • Adding the footnote

    This article describes how footnotes can be added to a MigraDoc document.

    Implementing footnotes

    In MigraDoc there is a Footnote class, but unfortunately rendering it to PDF is currently not supported. However, you can of course add footnotes using simple paragraphs and formatted texts (see Text). You can assign an own style to them (see Document / Formatting) and add hyperlinks from its references (see Hyperlinks). This chapter shows a sample implementation with some helper functions.

    Adding a footnote reference

    The following helper function AddFootnoteReference can be used to add a reference to a footnote. It uses a referenceId which identifies the footnote and equals the number to be shown in front of the footnote and at its references. The bookmark name here actually consists of the referenceId and a bookmarkPrefix to make it distinguishable from footnotes with the same numbers in other contexts.

    The function adds the referenceId in superscript, encapsulated in a link to the footnote, to the given paragraph. Additionally, it prepends a separator to avoid RTF specific problems (see comments inside the code).

    /// <summary>
    /// Adds the referenceId to the paragraph to refer to the footnote using its bookmark.
    /// </summary>
    void AddFootnoteReference(Paragraph paragraph, int referenceId, string bookmarkPrefix)
    {
        // If rendered to RTF at a decimal tab stop and directly after a number,
        // the following superscript referenceId would be interpreted as an exponent.
        // Due to this, the number and its so-called exponent would both be aligned
        // left of the decimal tab stop in RTF.
        // By inserting a small size space between them, we can avoid this.
        // The number will now be correctly aligned left of the decimal tab stop
        // and the referenceId will follow it after the tab stop.
        var separator = paragraph.AddFormattedText(" ");
        separator.Font.Size = Unit.FromPoint(1);
        separator.Font.Underline = Underline.None;
    
        // Get the bookmark name for this referenceId.
        var bookmarkName = GetFootnoteBookmarkName(referenceId, bookmarkPrefix);
    
        // Create a link to the footnote bookmark.
        var link = paragraph.AddHyperlink(bookmarkName);
        link.NoHyperlinkStyle = true;
    
        // Add the referenceId in superscript.
        var formattedText = link.AddFormattedText(referenceId.ToString());
        formattedText.Superscript = true;
    }
    

    See full Example in PDFsharp.Sample repository. Show resulting PDF

    Size is a property of the type Unit (see Formats / Unit for more information).

    AddFootnoteReference calls another helper function GetFootnoteBookmarkName. This concatenates the bookmarkPrefix and the referenceId to get a unique bookmark name.

    /// <summary>
    /// Gets the bookmark name by prepending the bookmarkPrefix to the referenceId.
    /// </summary>
    string GetFootnoteBookmarkName(int referenceId, string bookmarkPrefix)
    {
        return $"{bookmarkPrefix}_{referenceId}";
    }
    

    See full Example in PDFsharp.Sample repository. Show resulting PDF

    AddFootnoteReference can simply be called to add a footnote reference to a paragraph:

    // Define a unique prefix for the context of the following footnotes.
    var bookmarkPrefix = "NameValueTable";
    …
    var paragraph = row[1].AddParagraph("5");
                
    // Add a footnote reference to paragraph.
    AddFootnoteReference(paragraph, 1, bookmarkPrefix);
    

    See full Example in PDFsharp.Sample repository. Show resulting PDF

    Adding the footnote

    With the next helper function AddFootnote you can add a footnote after the last table or paragraph. It adds a new paragraph to the given section. This paragraph consists of the bookmark, the referenceId in superscript and the explaining text of the footnote. The bookmark name is determined using GetFootnoteBookmarkName and the referenceId and bookmarkPrefix parameters.

    /// <summary>
    /// Adds a footnote to the section.
    /// </summary>
    Paragraph AddFootnote(Section section, int referenceId, string bookmarkPrefix, string text)
    {
        // Add a paragraph in footnote style.
        var paragraph = section.AddParagraph();
        paragraph.Style = footnoteStyleName;
    
        // Get the bookmark name for this referenceId.
        var bookmarkName = GetFootnoteBookmarkName(referenceId, bookmarkPrefix);
    
        // Add the bookmark for the footnote.
        paragraph.AddBookmark(bookmarkName);
    
        // Add the referenceId in superscript.
        var formattedText = paragraph.AddFormattedText(referenceId.ToString());
        formattedText.Superscript = true;
    
        // Add the text to the footnote.
        paragraph.AddText($" {text}");
    
        return paragraph;
    }
    

    See full Example in PDFsharp.Sample repository. Show resulting PDF

    AddFootnote assigns a footnote style to the paragraph. This must be defined before:

    // Style for footnote.
    const string footnoteStyleName = "Footnote";
    var style = document.Styles.AddStyle(footnoteStyleName, StyleNames.Normal);
    style.Font.Size = Unit.FromPoint(8);
    

    See full Example in PDFsharp.Sample repository. Show resulting PDF

    Size is a property of the type Unit (see Formats / Unit for more information).

    AddFootnote can simply be called to add the footnote to a section:

    // Add the footnote and its text.
    AddFootnote(section, 1, bookmarkPrefix, "X must be a value between 1 and 10.");
    

    See full Example in PDFsharp.Sample repository. Show resulting PDF

    • Edit this page
    In this article
    Generated by DocFX  |   Privacy policy  |   Legal notice
    Back to top