Click or drag to resize

HeaderFooter Class

Represents a header or footer; allows to operate with headers and footers.
Inheritance Hierarchy

Namespace:  SautinSoft.Document
Assembly:  SautinSoft.Document (in SautinSoft.Document.dll) Version: 3.8.6.28 (3.8.6.28)
Syntax
public sealed class HeaderFooter : Element, 
	IContentElement

The HeaderFooter type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyCode exampleBlocks
Gets the header / footer content.
Public propertyContent
Gets the ContentRange of the current element.
(Inherited from Element.)
Public propertyDocument
Gets the owner document.
(Inherited from Element.)
Public propertyCode exampleElementType
Gets the ElementType of this element instance.
(Overrides ElementElementType.)
Public propertyCode exampleHeaderFooterType
Gets the type of the header / footer.
Public propertyIsHeader
Gets a value indicating whether this instance is header.
Public propertyNextSibling
Gets the next Element sibling.
(Inherited from Element.)
Public propertyParent
Gets the parent Section of this HeaderFooter instance.
Public propertyParentCollection
Gets the HeaderFooterCollection that contains this HeaderFooter instance.
Public propertyPreviuosSibling
Gets the previous Element sibling.
(Inherited from Element.)
Top
Methods
  NameDescription
Public methodCode exampleClone(Boolean)
Clones this HeaderFooter instance.
Public methodCode exampleClone(HeaderFooterType)
Clones this HeaderFooter instance and sets new HeaderFooterType.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodCode exampleGetChildElements(Boolean)
Gets the child elements.
(Inherited from Element.)
Public methodCode exampleGetChildElements(Boolean, ElementType)
Gets the child elements filtered by ElementType.
(Inherited from Element.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Explicit Interface Implementations
Examples
How to add a header and footer in the document using C#
using SautinSoft.Document;
using SautinSoft.Document.Drawing;

namespace Sample
{
    class Sample
    {

        static void Main(string[] args)
        {
            HeadersAndFooters();
        }

        /// <summary>
        /// How to add a header and footer into a document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/headersfooters.php
        /// </remarks>
        public static void HeadersAndFooters()
        {
            string documentPath = @"HeadersAndFooters.docx";

            // Let's create a simple document.
            DocumentCore dc = new DocumentCore();

            // Add a new section in the document.
            Section s = new Section(dc);
            dc.Sections.Add(s);

            // Let's add a paragraph with text.
            Paragraph p = new Paragraph(dc);
            dc.Sections[0].Blocks.Add(p);

            p.ParagraphFormat.Alignment = HorizontalAlignment.Justify;
            p.Content.Start.Insert("Once upon a time, in a far away swamp, there lived an ogre named Shrek whose precious " +
                                   "solitude is suddenly shattered by an invasion of annoying fairy tale characters...", new CharacterFormat() { Size = 12, FontName = "Arial" });

            // Create a new header with formatted text.
            HeaderFooter header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault);
            header.Content.Start.Insert("Shrek and Donkey travel to the castle and split up to find Fiona.", new CharacterFormat() { Size = 14.0 });

            // Add the header into HeadersFooters collection of the 1st section.
            s.HeadersFooters.Add(header);

            // Create a new footer with some text and image.
            HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault);

            // Create a paragraph to insert it into the footer.
            Paragraph par = new Paragraph(dc);
            par.Content.Start.Insert("Shrek and Donkey travel to the castle and split up to find Fiona. ", new CharacterFormat() { Size = 14.0 });
            par.ParagraphFormat.Alignment = HorizontalAlignment.Left;

            // Insert image into the paragraph.
            double wPt = LengthUnitConverter.Convert(7, LengthUnit.Centimeter, LengthUnit.Point);
            double hPt = LengthUnitConverter.Convert(7, LengthUnit.Centimeter, LengthUnit.Point);

            Picture pict = new Picture(dc, Layout.Inline(new Size(wPt, hPt)), @"..\..\image1.jpg");
            par.Inlines.Add(pict);

            // Add the paragraph into the Blocks collection of the footer.
            footer.Blocks.Add(par);

            // Finally, add the footer into 1st section (HeadersFooters collection).
            s.HeadersFooters.Add(footer);

            // Save the document into DOCX format.
            dc.Save(documentPath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath) { UseShellExecute = true });
        }
    }
}
How to add a header and footer in the document using VB.Net
Imports System
Imports System.IO
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing

Module Sample
    Sub Main()
        HeadersAndFooters()
    End Sub

    ''' <summary>
    ''' How to add a header and footer into a document.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/examples/headersfooters.php
    ''' </remarks>
    Sub HeadersAndFooters()
        Dim documentPath As String = "HeadersAndFooters.docx"

        ' Let's create a simple document.
        Dim dc As New DocumentCore()

        ' Add a new section in the document.
        Dim s As New Section(dc)
        dc.Sections.Add(s)

        ' Let's add a paragraph with text.
        Dim p As New Paragraph(dc)
        dc.Sections(0).Blocks.Add(p)

        p.ParagraphFormat.Alignment = HorizontalAlignment.Justify
        p.Content.Start.Insert("Once upon a time, in a far away swamp, there lived an ogre named Shrek whose precious " & "solitude is suddenly shattered by an invasion of annoying fairy tale characters...", New CharacterFormat() With {
            .Size = 12,
            .FontName = "Arial"
        })

        ' Create a new header with formatted text.
        Dim header As New HeaderFooter(dc, HeaderFooterType.HeaderDefault)
        header.Content.Start.Insert("Shrek and Donkey travel to the castle and split up to find Fiona.", New CharacterFormat() With {.Size = 14.0})

        ' Add the header into HeadersFooters collection of the 1st section.
        s.HeadersFooters.Add(header)

        ' Create a new footer with some text and image.
        Dim footer As New HeaderFooter(dc, HeaderFooterType.FooterDefault)

        ' Create a paragraph to insert it into the footer.
        Dim par As New Paragraph(dc)
        par.Content.Start.Insert("Shrek and Donkey travel to the castle and split up to find Fiona. ", New CharacterFormat() With {.Size = 14.0})
        par.ParagraphFormat.Alignment = HorizontalAlignment.Left

        ' Insert image into the paragraph.
        Dim wPt As Double = LengthUnitConverter.Convert(7, LengthUnit.Centimeter, LengthUnit.Point)
        Dim hPt As Double = LengthUnitConverter.Convert(7, LengthUnit.Centimeter, LengthUnit.Point)

        Dim pict As New Picture(dc, Layout.Inline(New Size(wPt, hPt)), "..\image1.jpg")
        par.Inlines.Add(pict)

        ' Add the paragraph into the Blocks collection of the footer.
        footer.Blocks.Add(par)

        ' Finally, add the footer into 1st section (HeadersFooters collection).
        s.HeadersFooters.Add(footer)

        ' Save the document into DOCX format.
        dc.Save(documentPath)

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(documentPath) With {.UseShellExecute = True})
    End Sub
End Module
See Also