Remove footnote from Word document in Java

We have already demonstrated how to use Spire.Doc to insert footnote to word document in Java applications. This article will show how to remove footnote from word document in Java.

Firstly, view the sample document with footnotes:

Remove footnote from Word document in Java

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.*;

public class RemoveFootnote {
    public static void main(String[] args) throws Exception {

        //Load the Sample Word document.
        Document document = new Document();
        document.loadFromFile("Sample.docx");

        Section section = document.getSections().get(0);

        //Traverse paragraphs in the section and find the footnote
        for (int j = 0; j < section.getParagraphs().getCount(); j++) {
            Paragraph para = section.getParagraphs().get(j);
            int index = -1;
            for (int i = 0, cnt = para.getChildObjects().getCount(); i < cnt; i++) {
                ParagraphBase pBase = (ParagraphBase) para.getChildObjects().get(i);
                if (pBase instanceof Footnote) {
                    index = i;
                    break;
                }
            }

            if (index > -1)
                //remove the footnote
                para.getChildObjects().removeAt(index);
        }
        document.saveToFile("Removefootnote.docx", FileFormat.Docx);
    }
}

Effective screenshot after remove the footnote from word document:

Remove footnote from Word document in Java