Headers and footers are generally used to display document name, date, page number, etc. In DsWord, 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, DsWord 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::
To add a header and footer to a section:
C# |
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 doc.Save("HeaderFooter.docx"); |
To modify a header and footer in a section:
C# |
Copy Code |
---|---|
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"); |
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.
C# |
Copy Code |
---|---|
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 DsWord, see DsWord sample browser.