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.

Yeah, but is there a way to achieve if you have two different type of headers then you can store two empty page?

@m.darshan.shah You can insert a page break into the main document body to produce one more empty page in the output document. As you may know MS Word documents are flow document, and there is no “page” concept. The consumer application reflows the document’s document into page on the fly.

but is there a way, If I know we have two header and footer and to create empty document of two page?

is there a way to share code where If I have two header and footer, so that I can insert and page break and get the empty the document with two header and footer and no content in it?

@m.darshan.shah You can use code like the following:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getFirstSection().getBody().removeAllChildren();
doc.ensureMinimum();
DocumentBuilder builder = new DocumentBuilder(doc);

// Check whether there are headers/footers for the first page
PageSetup ps = doc.getFirstSection().getPageSetup();
HeaderFooterCollection headerFooters = doc.getFirstSection().getHeadersFooters();
if (ps.getDifferentFirstPageHeaderFooter() &&
        (headerFooters.getByHeaderFooterType(HeaderFooterType.HEADER_FIRST) != null || headerFooters.getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST) != null))
    builder.insertBreak(BreakType.PAGE_BREAK);
// Do the same for odd/even headers/footers
if (ps.getOddAndEvenPagesHeaderFooter() &&
        (headerFooters.getByHeaderFooterType(HeaderFooterType.HEADER_EVEN) != null || headerFooters.getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN) != null))
    builder.insertBreak(BreakType.PAGE_BREAK);

doc.save("C:\\Temp\\out.docx");

@alexey.noskov Getting bunch of error while using this code.
example

HeaderFooterCollection’ does not contain a definition for ‘getByHeaderFooterType’ and no accessible extension method ‘getByHeaderFooterType’ accepting a first argument of type ‘HeaderFooterCollection’ could be found (are you missing a using directive or an assembly reference?)

is there a way to just store first two page break data instead of all the headers and footers?

@m.darshan.shah

Here is C# version of the code:

Document doc = new Document("C:\\Temp\\in.docx");
doc.FirstSection.Body.RemoveAllChildren();
doc.EnsureMinimum();
DocumentBuilder builder = new DocumentBuilder(doc);
// Check whether there are headers/footers for the first page
PageSetup ps = doc.FirstSection.PageSetup;
HeaderFooterCollection headerFooters = doc.FirstSection.HeadersFooters;
if (ps.DifferentFirstPageHeaderFooter &&
        (headerFooters[HeaderFooterType.HeaderFirst] != null || headerFooters[HeaderFooterType.FooterFirst] != null))
    builder.InsertBreak(BreakType.PageBreak);
// Do the same for odd/even headers/footers
if (ps.OddAndEvenPagesHeaderFooter &&
        (headerFooters[HeaderFooterType.HeaderEven] != null || headerFooters[HeaderFooterType.FooterEven] != null))
    builder.InsertBreak(BreakType.PageBreak);

doc.Save("C:\\Temp\\out.docx");

Unfortunately, it is not quite clear what you mean. Could you please elaborate?