Remove all Headers Footers and their Contents (Text Images Lines etc) from Word Document using C# Java

Hellow,Aspose.
When I use office2007 or office2010 , I can see a line in the Header, but I can not see it by office2016.
I use the function ‘Remove()’ in Class HeaderFooter , but I can not remove it.LineInHeader.zip (93.3 KB)
I want to know how to remove it by code.

@xuchen,

You can remove all headers/footers (including their content) from DOCX Word document by using the following C# code of Aspose.Words for .NET API:

Document doc = new Document(@"E:\\Temp\\LineInHeader\\in.docx");

foreach(Section sec in doc.Sections)
    sec.ClearHeadersFooters();          

doc.Save("E:\\Temp\\LineInHeader\\20.2.docx");

Output produced on our end is attached here for your reference:

Hope, this helps.

The ClearHeadersFooters function can not delete the line in Header.I can still see it by office2007.
Thank you for your Help

@xuchen,

Problematic line in Header will go away when you remove the “Header” style from source document:

Document doc = new Document(@"E:\\Temp\\LineInHeader\\in.docx");

foreach (Section sec in doc.Sections)
    sec.ClearHeadersFooters();

doc.Styles["Header"].Remove();

doc.Save("E:\\Temp\\LineInHeader\\20.2.docx"); 

Hope, this helps.

It still doesn’t work.
But I can delete it by Interop.word.dll,can you get me a similar method:

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
wordApp.Visible = false;
Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Open(filepath);

        wordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
        wordApp.Selection.ParagraphFormat.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;

        wordDoc.Close();
        wordApp.Quit();

@xuchen,

Please check, the following code will remove line style of bottom border from all the paragraphs in headers/footers of all the sections in Word document:

Document doc = new Document(@"E:\\Temp\\LineInHeader\\in.docx");

foreach (Section sec in doc.Sections)
    foreach (HeaderFooter hf in sec.HeadersFooters)
        foreach (Paragraph para in hf.GetChildNodes(NodeType.Paragraph, true))
            para.ParagraphFormat.Borders[BorderType.Bottom].LineStyle = LineStyle.None;

doc.Save("E:\\Temp\\LineInHeader\\20.2.docx");

Hope, this helps.

I‘m sorry this method still doesn’t work
I find if I use ‘Remove’ ,the line will show in office2007.

HeaderFooter header;
header = srcSection.HeadersFooters[HeaderFooterType.HeaderFirst];
if (header != null)
{
header.Remove();
}
header = srcSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
if (header != null)
{
header.Remove();
}
header = srcSection.HeadersFooters[HeaderFooterType.HeaderEven];
if (header != null)
{
header.Remove();
}

@xuchen,

Please check these input and output Word documents (Docs.zip (18.7 KB)). Do you still see line when opening 20.2.docx with MS Word 2007 on your end? I used the code from my previous post to produce 20.2.docx on my side. As shown in following screenshot, MS Word 2007 and MS Word 2019 both do not display any line in header:

Can you please also share the complete steps that we can follow to reproduce the same problem on our end?

Yes,the word documents you give me have no line in header.
If a word document has a line in header, I use shift+ctrl+n to delete in office2007. But the document will show the line in header after I remove it’s header by code if it had line before.
Program.zip (9.7 MB)

@xuchen,

Problematic line in Header will go away when you either remove the “Header” style from source document or apply the Normal Style to the problematic Paragraphs by using Ctrl+Shift+n shortcut key or modify the Header Style. Please try the following code to modify Header style. Hope, this helps:

Document doc = new Document(@"E:\\Temp\\Program\Program\WordDoc\\HeaderRemove.docx");
doc.Styles["Header"].ParagraphFormat.Borders[BorderType.Bottom].LineStyle = LineStyle.None;
doc.Save(@"E:\\Temp\\Program\Program\WordDoc\\20.2.docx");

The code will fix both HeaderRemove and LineStyleNone documents.

Oh,thank you for your help.It works.