Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Apr 29, 2024 6:09 am

Hi Team ,

I have written below mentioned code to exclude header and footer from first page .But It is not working.Please check and let me know what is wrong with this code


Added Two sections in the Document :
homeSection = document.addSection();
section = document.addSection();


To remove Header and footer:
HeaderFooter firstPageHeader = document.getSections().get(0).getHeadersFooters().getHeader();
firstPageHeader.getChildObjects().clear();
firstPageHeader.addParagraph();

HeaderFooter firstPageFooter = document.getSections().get(0).getHeadersFooters().getFooter();
firstPageFooter.getChildObjects().clear();
firstPageFooter.addParagraph();


Thanks in advance

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Mon Apr 29, 2024 6:29 am

Hi,

Thanks for your inquiry.
For your requirement, you need to set setDifferentFirstPageHeaderFooter to true, than use getFirstPageHeader and getFirstPageFooter method to obtain the header and footer of the first page. I put the complete code below for your reference:

Code: Select all
// Get the first section of the document
Section section = doc.getSections().get(0);
// Set the first page header and footer to be different from the rest of the pages
section.getPageSetup().setDifferentFirstPageHeaderFooter(true);

// Get the first page header of the section
HeaderFooter firstPageHeader = section.getHeadersFooters().getFirstPageHeader();
// Clear any existing child objects in the first page header
firstPageHeader.getChildObjects().clear();
// Add a new paragraph to the first page header
firstPageHeader.addParagraph();

// Get the first page footer of the section
HeaderFooter firstPageFooter = section.getHeadersFooters().getFirstPageFooter();
// Clear any existing child objects in the first page footer
firstPageFooter.getChildObjects().clear();
// Add a new paragraph to the first page footer
firstPageFooter.addParagraph();


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc

cron