[]
        
(Showing Draft Content)

Header and Footer

Headers and footers are generally used to display document name, date, page number, etc. In GcWord, headers and footers of a section are represented by the HeaderFooter class which provides access to the content and settings of header and footer. These headers and footers have their own body which represents the header and footer content. The HeaderFooter is not a part of the document body and it does not get derived from the ContentObject class, hence it is not a content object. The header and footer of a particular section can be accessed using  Headers and Footers properties of the Section class which are of type HeaderFooterCollection class.

In addition, GcWord provides three different types of headers and footers through HeaderFooterType enumeration, which are listed below:

  • Primary - The header or footer that appears on the odd pages.

  • EvenPage - The header or footer that appears on the even pages.

  • FirstPage - The header that appears only on the first page. When the first page header is not set, the primary header is displayed on the first page.

However, rendering of headers and footers in a document depends on the following properties::

  • LinkToPrevious - This property is provided by the HeaderFooter class. It accepts boolean value to determine whether the header/footer is linked to the previous section. When the value of this property is set to true for a section, then the section displays the same header and footer as the previous one.

  • DifferentFirstPageHeaderFooter - This property is provided by the PageSetup class. It accepts boolean value to determine whether the section should have a different header and footer for its first page or not.

  • OddAndEvenPagesHeaderFooter - This property is provided by the PageSetup class and accepts boolean value. When set to true, Primary headers/footers are displayed for odd-numbered pages and EvenPage headers/footers are displayed for even-numbered pages. When set to false, the Primary type header/footer is displayed on all the pages of a section. Note that changing the value of this property affects all the section in a document.

Word document with a header and footer

Add Header and Footer

To add a header and footer to a section:

  1. Access a section where you want to add a header and footer. For example, access first section of the document.

  2. Use the Headers and Footers properties of the Section class to access the header and footer collection of the section.

  3. Add header and footer, by using the Add method, on different pages, for example, first page, even page, and odd page.

    var section = doc.Body.Sections.First;
    
    //Set different header and footer for the first page of the section
    section.PageSetup.DifferentFirstPageHeaderFooter = true;
    
    //Insert first page header and footer
    section.Headers[HeaderFooterType.FirstPage].Body.Paragraphs.Add("Section1: FirstPage Header");
    section.Footers[HeaderFooterType.FirstPage].Body.Paragraphs.Add("Section1: FirstPage Footer");
    
    //Insert header and footer for odd pages
    section.Headers[HeaderFooterType.Primary].Body.Paragraphs.Add("Section1: Odd Page Header");
    section.Footers[HeaderFooterType.Primary].Body.Paragraphs.Add("Section1: Odd Page Footer");
    
    //Insert header and footer for even pages
    section.Headers[HeaderFooterType.EvenPages].Body.Paragraphs.Add("Section1: Even Page Header");
    section.Footers[HeaderFooterType.EvenPages].Body.Paragraphs.Add("Section1: Even Page Footer");
    
    //Add a paragraph to the section
    for (var p = 1; p <= 50; p++)
    {
        section.GetRange().Paragraphs.Add("Section1: Test Paragraph" + p.ToString());
    }
    
    //Add second section
    var section2 = doc.Body.Sections.Add();
    section2.PageSetup.SectionStart = SectionStart.NewPage;
    section2.PageSetup.OddAndEvenPagesHeaderFooter = true;
    
    //Link the header and footer of pages with the previous section
    section2.Headers[HeaderFooterType.Primary].LinkToPrevious = false;
    section2.Footers[HeaderFooterType.Primary].LinkToPrevious = false;
    section2.Headers[HeaderFooterType.EvenPages].LinkToPrevious = false;
    section2.Footers[HeaderFooterType.EvenPages].LinkToPrevious = false;
    
    //Insert header and footer for odd and even pages of the second section
    section2.Headers[HeaderFooterType.Primary].Body.Paragraphs.Add("Section2: Odd Page Header");
    section2.Footers[HeaderFooterType.Primary].Body.Paragraphs.Add("Section2: Odd Page Footer");
    section2.Headers[HeaderFooterType.EvenPages].Body.Paragraphs.Add("Section2: Even Page Header");
    section2.Footers[HeaderFooterType.EvenPages].Body.Paragraphs.Add("Section2: Even Page Footer");
    
    //Add a paragraph to the second section
    for (var p = 1; p <= 75; p++)
    {
        section2.GetRange().Paragraphs.Add("Section2: Test Paragraph" + p.ToString());
    }
    
    //Save the document
    doc.Save("HeaderFooter.docx");

Modify Header and Footer

To modify a header and footer in a section:

  1. Access the section whose header and footer needs to be modified. For example, access first section of the document

  2. Access the header and footer collection using Headers and Footers properties of the Section class.

  3. Modify text of the header and footer added to the first page using Value property of the Text class.

    doc.Load("HeaderFooter.docx");
    
    //Access the first section
    var section = doc.Body.Sections.First;
    
    //Modify the header and footer on the first page
    section.Headers[HeaderFooterType.FirstPage].Body.Texts.First.Value = 
        "Modified first page header";
    section.Footers[HeaderFooterType.FirstPage].Body.Texts.First.Value = 
        "Modified first page footer";
    
    //Save the document
    doc.Save("ModifiedHeaderFooter.docx");

Delete Header and Footer

To delete the content of a header and/or footer from a section, you can use Delete method of the ContentObject class. Alternatively, you can clear content from the header and footer using Clear method of the Body class.

doc.Load("HeaderFooter.docx");

var section = doc.Body.Sections.First;
//Clear the primary header and footer content in the first section
section.Headers[HeaderFooterType.Primary].Body.Clear();
section.Footers[HeaderFooterType.Primary].Body.Clear();

//Delete the primary header's and footer's first paragraph in the second section
var section2 = doc.Body.Sections[1];
section2.Headers[HeaderFooterType.Primary].Body.Paragraphs.First.Delete();
section2.Footers[HeaderFooterType.Primary].Body.Paragraphs.First.Delete();

//Save the document
doc.Save("DeletedHeaderFooter.docx");

For more information on how to implement header and footer in a Word document using GcWord, see GcWord sample browser.