How to clear Header Footer deep?

How to achieve this function of clearing all formatting in word, I want to clear all the header and page numbers, and the table borders inside are also clear.

@Crane

To clear all headers, footers, and formatting such as page numbers and table borders in a Word document using Aspose.Words for Java, you can follow the example below. This code demonstrates how to remove all types of headers and footers from each section of the document:

import com.aspose.words.Document;
import com.aspose.words.HeaderFooter;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.Section;
import com.aspose.words.examples.Utils;

public class ClearHeaderFooter {
    public static void main(String[] args) throws Exception {
        // Specify the path to your document
        String dataDir = Utils.getDataDir(ClearHeaderFooter.class);
        Document doc = new Document(dataDir + "YourDocument.docx");

        for (Section section : doc.getSections()) {
            // Remove all types of headers and footers
            for (HeaderFooterType type : HeaderFooterType.values()) {
                HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(type);
                if (header != null) {
                    header.remove();
                }
            }
        }

        // Save the modified document
        doc.save(dataDir + "HeadersFootersCleared.docx");
        System.out.println("Headers and footers cleared successfully.");
    }
}

Explanation:

  1. Document Loading: The document is loaded from a specified path.
  2. Iterate Sections: The code iterates through each section of the document.
  3. Remove Headers and Footers: It checks for all types of headers and footers (first, primary, and even) and removes them if they exist.
  4. Save Document: Finally, the modified document is saved to a new file.

This approach will effectively clear all headers and footers from your Word document. If you also want to clear table borders or other formatting, you would need to iterate through the tables and set their borders to null or a default state.

Feel free to ask if you need further assistance or additional examples!

Sources:
[1]: AsposeHeaderFooterRemove.java

When all are deleted, a Y header with a lower border appears.

@Crane It looks like you simply need to clear formatting applied to nodes. You can achieve this using ParagraphFormat.clearFormatting and Font.clearFormatting methods. For example you can use DocumentVisitor to clear formatting in the whole document:

Document doc = new Document("C:\\Temp\\in.docx");
doc.accept(new FormattingCleaner());
doc.save("C:\\Temp\\out.docx");
public class FormattingCleaner extends DocumentVisitor {
    
    /// <summary>
    /// Called when a FieldEnd node is encountered in the document.
    /// </summary>
    @Override
    public int visitFieldEnd(FieldEnd fieldEnd)
    {
        //Simply change font name
        resetFont(fieldEnd.getFont());
        return VisitorAction.CONTINUE;
    }
    
    /// <summary>
    /// Called when a FieldSeparator node is encountered in the document.
    /// </summary>
    @Override
    public int visitFieldSeparator(FieldSeparator fieldSeparator)
    {
        resetFont(fieldSeparator.getFont());
        return VisitorAction.CONTINUE;
    }
    
    /// <summary>
    /// Called when a FieldStart node is encountered in the document.
    /// </summary>
    @Override
    public int visitFieldStart(FieldStart fieldStart)
    {
        resetFont(fieldStart.getFont());
        return VisitorAction.CONTINUE;
    }
    
    /// <summary>
    /// Called when a Footnote end is encountered in the document.
    /// </summary>
    @Override
    public int visitFootnoteEnd(Footnote footnote)
    {
        resetFont(footnote.getFont());
        return VisitorAction.CONTINUE;
    }
    
    /// <summary>
    /// Called when a FormField node is encountered in the document.
    /// </summary>
    @Override
    public int visitFormField(FormField formField)
    {
        resetFont(formField.getFont());
        return VisitorAction.CONTINUE;
    }
    
    /// <summary>
    /// Called when a Paragraph end is encountered in the document.
    /// </summary>
    @Override
    public int visitParagraphEnd(Paragraph paragraph)
    {
        paragraph.getParagraphFormat().clearFormatting();
        resetFont(paragraph.getParagraphBreakFont());
        return VisitorAction.CONTINUE;
    }
    
    /// <summary>
    /// Called when a Run node is encountered in the document.
    /// </summary>
    @Override
    public int visitRun(Run run)
    {
        resetFont(run.getFont());
        return VisitorAction.CONTINUE;
    }
    
    /// <summary>
    /// Called when a SpecialChar is encountered in the document.
    /// </summary>
    @Override
    public int visitSpecialChar(SpecialChar specialChar)
    {
        resetFont(specialChar.getFont());
        return VisitorAction.CONTINUE;
    }
    
    private void resetFont(Font font)
    {
        font.clearFormatting();
    }
}

@alexey.noskov paragraph.getParagraphFormat().getBorders().clearFormatting();

To clear it like this, is there a way to clear all the formatting of the paragraph, including others, such as this border, I’m afraid he has other things to clear

@Crane Unfortunately, it is not quite clear what border you are talking about. Could you please attach your input, output and expected output documents here for our reference? We will check your documents and provide you more information.