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.
@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");
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 ?
@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 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.
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?
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?)
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?