How to remove header and footer from word file on specific page using doc io

How to remove the header and footer from word file on specific page using doc io

1 Reply 1 reply marked as answer

MJ Mohanaselvam Jothi Syncfusion Team March 9, 2021 09:49 AM UTC

Hi Hardik,

Thank you for contacting Syncfusion support.

The Word document is a flow document in which contents will not be preserved page by page; instead the contents will be preserved sequentially section by section.

Word viewer/editor renders the contents of the Word document page by page dynamically when opened for viewing or editing. This page wise rendered information will not be preserved in the saved file level. 

Essential DocIO is a non-UI component that provides a full-fledged document object model (logical structure as defined in the file level) to manipulate the Word document contents without converting page by page. So, it is not feasible to identify the page using Word library (DocIO).

Using DocIO, you can identify the sections in the Word document and remove the headers and footers from the sections. Please refer the below code example to remove headers and footers in the Word document: 
//Opens an existing document  
FileStream fileStreamPath = new FileStream(@"Input.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic); 
//Iterate to each sections in Word document 
foreach(WSection section in document.Sections) 
{ 
    //Remove odd header 
    section.HeadersFooters.OddHeader.ChildEntities.Clear(); 
    //Remove even header 
    section.HeadersFooters.EvenHeader.ChildEntities.Clear(); 
    //Remove first page header 
    section.HeadersFooters.FirstPageHeader.ChildEntities.Clear(); 
    //Remove odd footer 
    section.HeadersFooters.OddFooter.ChildEntities.Clear(); 
    //Remove even footer 
    section.HeadersFooters.EvenFooter.ChildEntities.Clear(); 
    //Remove first page footer 
    section.HeadersFooters.FirstPageFooter.ChildEntities.Clear(); 
} 
//Saving the Word document 
FileStream outputStream = new FileStream("Sample.docx", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); 
document.Save(outputStream, FormatType.Docx); 
document.Close(); 
outputStream.Flush(); 
outputStream.Dispose(); 

Please refer the below documentation to know more about working with headers and footers using DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-sections#working-with-headers-and-footers

Please let us know if you have any other questions.

Regards,
Mohanaselvam J 


Marked as answer
Loader.
Up arrow icon