How to remove all the content from a document but should retain the page boarder, header and footer

Hi,

I am using a upload document and then will change some special words with values using IReplace interface. For certain case I have to remove all the content after replacing it, but should show the exact empty pages(page number should be same after removing the content). Also after emptying the page page boarder, header and footer shouldn’t remove. So can you please help me to figure out a solution for this problem.

Thank you

@Gptrnt MS Word document consist of Sections. Each section has Headers/Footers and the main body. So if the document contains only one section and it is required to remove everything except Header/Footer, you should simply remove all content from the section’s body. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getFirstSection().getBody().removeAllChildren();
doc.save("C:\\Temp\\out.docx");

Please see our documentation to learn more about Aspose.Words Document Object model:
https://docs.aspose.com/words/java/aspose-words-document-object-model/

Hi,

Sometimes my document contains more than one section in the content, So in that case I need to find the section which has header and footer. Is there any property to find it ?

Thank you

@Gptrnt You can check the headers/footers count in the section to determine whether it has headers/footers or not:

boolean hasHeadersFooter = doc.getFirstSection().getHeadersFooters().getCount()>0;

@m.darshan.shah You can use provided solution to remove content only from a body. But all header/footer settings and text will be preserved.

my bad, its working

@vyacheslav.deryushev there is one problem if you have two different headers and footers like odd and even page or 1st page and rest page has same.
it doesn’t store both the headers and footers
cc: @alexey.noskov

@m.darshan.shah If you remove all body content, there will be only one page in the section so only one type of header/footer can be displayed.