How to get footnote numbers without square brackets

Hi Team,

When I convert a Word document to HTML, the footnotes appear as [1], [2], etc. Why are the square brackets included in the HTML output? How can I remove them? I want the footnote numbers to appear without brackets.

Code :

HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
    ExportImagesAsBase64 = true,
    ExportXhtmlTransitional = true,
    Encoding = System.Text.Encoding.UTF8,
    ExportHeadersFootersMode = ExportHeadersFootersMode.None,
    ExportPageMargins = true,
    ExportPageSetup = true,
    UseHighQualityRendering = false,
    AllowEmbeddingPostScriptFonts = true,
    PrettyFormat = false,
    SaveFormat = SaveFormat.Html,
    ExportTocPageNumbers = true,
    CssStyleSheetType = CssStyleSheetType.Inline,
    MemoryOptimization = true,
    ExportListLabels = ExportListLabels.AsInlineText
};
var loadOption = new Aspose.Words.Loading.LoadOptions() { LoadFormat = LoadFormat.Docx };
using (MemoryStream wordMs = new MemoryStream(wordBytes))
{
    Document doc = new Document(wordMs, loadOption);
    doc.Cleanup();
    doc.WebExtensionTaskPanes.Clear();
    doc.RemoveExternalSchemaReferences();
    doc.RemoveMacros();

    doc.Save(@"D:\Temp\NohaIssue\FootenoteSample.html", options);
}

Source Document :
FootenoteSample.docx (15.7 KB)

Output :
FootenoteSample.zip (1.2 KB)

@AlpeshChaudhariDev

To remove the square brackets from footnote numbers when converting a Word document to HTML using Aspose.Words, you can customize the footnote formatting in your document before saving it. Unfortunately, the default behavior of Aspose.Words includes square brackets around footnote references in the HTML output.

Here’s a general approach you can take:

  1. Modify the Footnote Reference Formatting: You can iterate through the footnotes in your document and change their formatting to remove the brackets. This can be done by accessing the Footnote objects and modifying their FootnoteType or using a custom DocumentBuilder to format the text.

  2. Save the Document: After modifying the footnote references, save the document using the HtmlSaveOptions as you have already done in your code.

Here’s a code snippet that demonstrates how to remove the square brackets from footnote references:

using Aspose.Words;
using Aspose.Words.Saving;

public void ConvertWordToHtmlWithoutBrackets(byte[] wordBytes)
{
    var loadOptions = new LoadOptions() { LoadFormat = LoadFormat.Docx };
    using (MemoryStream wordMs = new MemoryStream(wordBytes))
    {
        Document doc = new Document(wordMs, loadOptions);
        
        // Iterate through footnotes and modify their reference formatting
        foreach (Footnote footnote in doc.GetChildNodes(NodeType.Footnote, true))
        {
            // Assuming you want to remove brackets from the footnote reference
            footnote.RemoveAllChildren(); // Clear existing content
            footnote.AppendChild(new Run(doc, footnote.Index.ToString())); // Add the index as plain text
        }

        // Save the document to HTML
        HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
        {
            ExportImagesAsBase64 = true,
            ExportXhtmlTransitional = true,
            Encoding = System.Text.Encoding.UTF8,
            ExportHeadersFootersMode = ExportHeadersFootersMode.None,
            ExportPageMargins = true,
            ExportPageSetup = true,
            UseHighQualityRendering = false,
            AllowEmbeddingPostScriptFonts = true,
            PrettyFormat = false,
            CssStyleSheetType = CssStyleSheetType.Inline,
            MemoryOptimization = true,
            ExportListLabels = ExportListLabels.AsInlineText
        };

        doc.Save(@"D:\Temp\FootnoteSample.html", options);
    }
}

In this example, the footnote references are modified to display only the index number without any brackets. Adjust the logic as necessary to fit your specific requirements.

If you have any further questions or need additional assistance, feel free to ask!

@AlpeshChaudhariDev Unfortunately, currently there is no way to avoid square brackets in footnote numbers. You can try postprocessing your output HTML document to remove them.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-27835

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Can I keep the content of the footnote while removing the footnote node itself ?
Is that possible? I want to retain the content, as shown in the screenshot below.

@AlpeshChaudhariDev You can try using the following code to replace footnotes with simple text:

Document doc = new Document("C:\\Temp\\in.docx");
// Update footnote markers.
doc.UpdateActualReferenceMarks();
DocumentBuilder builder = new DocumentBuilder(doc);

int bkIndex = 0;
foreach (Footnote note in doc.GetChildNodes(NodeType.Footnote, true))
{
    // Insert bookmark at the footnote.
    // Note: bookmarks, which names starts from underscore are hidden in MS Word.
    string bkName = "_footnote" + bkIndex;
    bkIndex++;
    note.ParentNode.InsertBefore(new BookmarkStart(doc, bkName), note);
    note.ParentNode.InsertAfter(new BookmarkEnd(doc, bkName), note);
    builder.MoveToBookmark(bkName, true, true);
    // Insert footnote number.
    builder.Font.Superscript = true;
    builder.Write(note.ActualReferenceMark);

    foreach (Node child in note.GetChildNodes(NodeType.Any, false))
    {
        Node newNode = child.Clone(true);
        doc.LastSection.Body.AppendChild(newNode);
        // Replace footnote ref special char with hyperlink to the bookmark.
        if (newNode.IsComposite)
        {
            CompositeNode newComposite = (CompositeNode)newNode;
            foreach (SpecialChar sc in newComposite.GetChildNodes(NodeType.SpecialChar, true))
            {
                // This is a footnote character
                if (sc.GetText().Equals("\u0002"))
                {
                    builder.MoveTo(sc);
                    builder.InsertHyperlink(note.ActualReferenceMark, bkName, true);
                    sc.Remove();
                }
            }
        }
    }
}

// Remove all footnotes.
doc.GetChildNodes(NodeType.Footnote, true).Clear();

doc.Save("C:\\Temp\\out.docx");
doc.Save("C:\\Temp\\out.html");
1 Like

Thanks for the resposne.

UpdateActualReferenceMarks() and ActualReferenceMark is not available in Aspose.Words version 21.8.0

@AlpeshChaudhariDev These method and property have been introduced in 24.2 version of Aspose.Words.

Any alternate method and property available or not in Version 21.8.0

@AlpeshChaudhariDev I am afraid, there is no alternative in older version of Aspose.Words. So I would suggest you to update to the newer version.

When I used the above code to replace footnotes with plain text, the footnote reference moved to the end of the section. I want to keep the content at the end of the page (original position), not at the end of the section. How can I achieve this?

@AlpeshChaudhariDev I am afraid there is no direct way to achieve this. You can try splitting the document into pages using Document.ExtractPages method before replacing footnotes with plain text and then rejoin the documents before conversion to HTML.

i have splitting the document using the bellow code but still the footnote is visible at the end of document :

HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
    ExportImagesAsBase64 = true,
    ExportXhtmlTransitional = true,
    Encoding = System.Text.Encoding.UTF8,
    ExportHeadersFootersMode = ExportHeadersFootersMode.None,
    ExportPageMargins = true,
    ExportPageSetup = true,
    UseHighQualityRendering = false,
    AllowEmbeddingPostScriptFonts = true,
    PrettyFormat = false,
    SaveFormat = SaveFormat.Html,
    ExportTocPageNumbers = true,
    CssStyleSheetType = CssStyleSheetType.Inline,
    MemoryOptimization = true,
    ExportListLabels = ExportListLabels.AsInlineText
};
var loadOption = new Aspose.Words.Loading.LoadOptions() { LoadFormat = LoadFormat.Docx };
using (MemoryStream wordMs = new MemoryStream(wordBytes))
{
    Document doc = new Document(wordMs, loadOption);
    doc.Cleanup();
    doc.WebExtensionTaskPanes.Clear();
    doc.RemoveExternalSchemaReferences();
    doc.RemoveMacros();
    Aspose.Words.Document tempDoc = (Aspose.Words.Document)doc.Clone(false);
    int pageNumber = 1;

    for (int i = 0; i < doc.PageCount; i++)
    {
        //Console.WriteLine("page number :" + i + " started");
        Aspose.Words.Document page = doc.ExtractPages(i, 1);

        // Remove section breaks in the page.
        while (page.Sections.Count > 1)
        {
            page.FirstSection.AppendContent(page.Sections[1]);
            page.Sections[1].Remove();
        }
        // Reset section start of the section.
        page.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
        tempDoc.AppendDocument(page, ImportFormatMode.UseDestinationStyles);
        page = null;
    }
    tempDoc.UpdatePageLayout();
    tempDoc.Save(@"D:\Temp\FootenoteTest.html", options);
}

Source Doc :
ENI SPA From Stage.docx (2.2 MB)

Output :
FootenoteTest.zip (3.2 MB)

@AlpeshChaudhariDev But you do not convert footnotes to text in your code. It is expected that footnotes are moved to the end of the document when document is converted to HTML, because HTML is flow by its nature and there is no concept of “page”.
If you would like to export paginated HTML, you can consider exporting your document to HtmlFixed format.