Documents for Word, .NET Edition
Features / Header and Footer
In This Topic
    Header and Footer
    In This Topic

    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:

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

    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.
      Copy Code
      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
    Back to Top

    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.
      Copy Code
      //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
    Back to Top

    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.

    Copy Code
    var section = doc.Body.Sections.First;
    //Clear the primary header and footer content in the first section
    //Delete the primary header's and footer's first paragraph in the second section
    var section2 = doc.Body.Sections[1];
    //Save the document
    Back to Top

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