Header and Footer

  • Demo
  • C# source
  • VB.Net source
This demo shows you how to set the page size, page header and footer of the Word document. We show you also set some picture as the background of the header/footer.
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the demo you want, please contact support@e-iceblue.com for the free customized demo just for you.
using System;
using System.Drawing;

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace DemoOnlineCode
{
    class HeaderAndFooter
    {
        public void demoHeaderAndFooter(String docFile,
            String headerImageFile = null,
            String footerImageFile = null,
            string pageSizeName = "A4",
            String headerFontColor = null,
            String footerFontColor = null)
        {
            Document document = new Document(docFile, FileFormat.Auto);
            SizeF pageSize = (SizeF)typeof(PageSize).GetField(pageSizeName).GetValue(null);
            PageSetup(document, pageSize);
            SetHeader(document, ConvertStrToColor(headerFontColor), headerImageFile);
            SetFooter(document,ConvertStrToColor(footerFontColor), footerImageFile);
            document.SaveToFile("demo.doc", FileFormat.Doc);

        }

        private void PageSetup(Document document, SizeF pageSize)
        {
            for (int i = 0; i < document.Sections.Count; i++)
            {
                Section section = document.Sections[i];
                section.PageSetup.PageSize = pageSize;
                section.PageSetup.Margins.Top = (float)72.0;
                section.PageSetup.Margins.Bottom = (float)72.0;
                section.PageSetup.Margins.Left = (float)89.0;
                section.PageSetup.Margins.Right = (float)89.0;
            }
        }

        private void SetFooter(Document document,
            Color fontColor,
            string footerImageFile = null,
            string footerText = "Spire.Doc",
            string fontName = "Calibri",
            float fontSize = (float)12.0,
            HorizontalAlignment alignment = HorizontalAlignment.Left)
        {
            for (int i = 0; i < document.Sections.Count; i++)
            {
                Section section = document.Sections[i];
                HeaderFooter footer = section.HeadersFooters.Footer;
                Paragraph footerParagraph = footer.AddParagraph();
                TextRange text = footerParagraph.AppendText(footerText);

                text.CharacterFormat.FontName = fontName;
                text.CharacterFormat.FontSize = fontSize;
                text.CharacterFormat.TextColor = fontColor;
                footerParagraph.Format.HorizontalAlignment = alignment;

                //border
                footerParagraph.Format.Borders.Top.BorderType
                    = (BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline;
                footerParagraph.Format.Borders.Top.Space = (float)0.03;
                if (footerImageFile != null)
                {
                    Byte[] imagedata = System.IO.File.ReadAllBytes(footerImageFile);
                    //insert picture to footer
                    DocPicture footerPicture = footerParagraph.AppendPicture(imagedata);

                    //footer picture layout
                    footerPicture.TextWrappingStyle = TextWrappingStyle.Behind;
                    footerPicture.HorizontalOrigin = HorizontalOrigin.Page;
                    footerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;
                    footerPicture.VerticalOrigin = VerticalOrigin.Page;
                    footerPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom;

                }
                //insert page number
                footerParagraph = footer.AddParagraph();
                footerParagraph.AppendField("page number", FieldType.FieldPage);
                footerParagraph.AppendText("of");
                footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
                footerParagraph.Format.HorizontalAlignment
                    = (HorizontalAlignment)HorizontalAlignment.Right;
            }
        }

        private void SetHeader(Document document,
            Color fontColor,
            string headerImageFile = null,
            string headerText = "E-iceblue",
            string fontName = "Calibri",
            float fontSize = (float)12.0,
            HorizontalAlignment alignment = HorizontalAlignment.Center)
        {
            for (int i = 0; i < document.Sections.Count; i++)
            {
                Section section = document.Sections[i];
                HeaderFooter header = section.HeadersFooters.Header;

                //insert picture and text to header
                Paragraph headerParagraph = header.AddParagraph();
                if (headerImageFile != null)
                {
                    Byte[] imagedata = System.IO.File.ReadAllBytes(headerImageFile);
                    DocPicture headerPicture = headerParagraph.AppendPicture(imagedata);

                    //header picture layout - text wrapping
                    headerPicture.TextWrappingStyle = TextWrappingStyle.Behind;

                    //header picture layout - position
                    headerPicture.HorizontalOrigin = HorizontalOrigin.Page;
                    headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;
                    headerPicture.VerticalOrigin = VerticalOrigin.Page;
                    headerPicture.VerticalAlignment = ShapeVerticalAlignment.Top;
                }

                //header text
                TextRange text = headerParagraph.AppendText(headerText);
                text.CharacterFormat.FontName = fontName;
                text.CharacterFormat.FontSize = Convert.ToSingle(fontSize);
                text.CharacterFormat.TextColor = fontColor;
                headerParagraph.Format.HorizontalAlignment = alignment;

                //border
                headerParagraph.Format.Borders.Bottom.BorderType
                    = (Spire.Doc.Documents.BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline;
                headerParagraph.Format.Borders.Bottom.Space = (float)0.03;

            }
        }

        private Color ConvertStrToColor(string strColor)
        {
            if (String.IsNullOrWhiteSpace(strColor))
            {
                return Color.Empty;
            }
            else
            {
                return ColorTranslator.FromHtml("#" + strColor);
            }
        }

    }
}
Imports System.Drawing

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace DemoOnlineCode
    Class HeaderAndFooter
        Public Sub demoHeaderAndFooter(docFile As [String],
                                       Optional headerImageFile As [String] = Nothing,
                                       Optional footerImageFile As [String] = Nothing,
                                       Optional pageSizeName As String = "A4",
                                       Optional headerFontColor As [String] = Nothing,
                                       Optional footerFontColor As [String] = Nothing)
            Dim document As New Document(docFile, FileFormat.Auto)
            Dim pageSize As SizeF _
                = CType(GetType(PageSize).GetField(pageSizeName).GetValue(Nothing), SizeF)
            PageSetup(document, pageSize)
            SetHeader(document, ConvertStrToColor(headerFontColor), headerImageFile)
            SetFooter(document, ConvertStrToColor(footerFontColor), footerImageFile)
            document.SaveToFile("demo.doc", FileFormat.Doc)

        End Sub
        Private Sub PageSetup(document As Document, pageSize As SizeF)
            For i As Integer = 0 To document.Sections.Count - 1
                Dim section As Section = document.Sections(i)
                section.PageSetup.PageSize = pageSize
                section.PageSetup.Margins.Top = CSng(72.0)
                section.PageSetup.Margins.Bottom = CSng(72.0)
                section.PageSetup.Margins.Left = CSng(89.0)
                section.PageSetup.Margins.Right = CSng(89.0)
            Next
        End Sub
        Private Sub SetFooter(document As Document,
                              fontColor As Color,
                              Optional footerImageFile As String = Nothing,
                              Optional footerText As String = "Spire.Doc",
                              Optional fontName As String = "Calibri",
                              Optional fontSize As Single = CSng(12.0), _
                              Optional alignment As HorizontalAlignment = HorizontalAlignment.Left)
            For i As Integer = 0 To document.Sections.Count - 1
                Dim section As Section = document.Sections(i)
                Dim footer As HeaderFooter = section.HeadersFooters.Footer
                Dim footerParagraph As Paragraph = footer.AddParagraph()
                Dim text As TextRange = footerParagraph.AppendText(footerText)

                text.CharacterFormat.FontName = fontName
                text.CharacterFormat.FontSize = fontSize
                text.CharacterFormat.TextColor = fontColor
                footerParagraph.Format.HorizontalAlignment = alignment

                'border
                footerParagraph.Format.Borders.Top.BorderType _
                    = DirectCast(Spire.Doc.Documents.BorderStyle.Hairline, BorderStyle)
                footerParagraph.Format.Borders.Top.Space = CSng(0.03)
                If footerImageFile IsNot Nothing Then
                    Dim imagedata As [Byte]() = System.IO.File.ReadAllBytes(footerImageFile)
                    'insert picture to footer
                    Dim footerPicture As DocPicture = footerParagraph.AppendPicture(imagedata)

                    'footer picture layout
                    footerPicture.TextWrappingStyle = TextWrappingStyle.Behind
                    footerPicture.HorizontalOrigin = HorizontalOrigin.Page
                    footerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left
                    footerPicture.VerticalOrigin = VerticalOrigin.Page

                    footerPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom
                End If
                'insert page number
                footerParagraph = footer.AddParagraph()
                footerParagraph.AppendField("page number", FieldType.FieldPage)
                footerParagraph.AppendText("of")
                footerParagraph.AppendField("number of pages", FieldType.FieldNumPages)
                footerParagraph.Format.HorizontalAlignment _
                    = DirectCast(HorizontalAlignment.Right, HorizontalAlignment)
            Next
        End Sub
        Private Sub SetHeader(document As Document,
                              fontColor As Color,
                              Optional headerImageFile As String = Nothing,
                              Optional headerText As String = "E-iceblue",
                              Optional fontName As String = "Calibri",
                              Optional fontSize As Single = CSng(12.0), _
         Optional alignment As HorizontalAlignment = HorizontalAlignment.Center)
            For i As Integer = 0 To document.Sections.Count - 1
                Dim section As Section = document.Sections(i)
                Dim header As HeaderFooter = section.HeadersFooters.Header

                'insert picture and text to header
                Dim headerParagraph As Paragraph = header.AddParagraph()
                If headerImageFile IsNot Nothing Then
                    Dim imagedata As [Byte]() = System.IO.File.ReadAllBytes(headerImageFile)
                    Dim headerPicture As DocPicture = headerParagraph.AppendPicture(imagedata)

                    'header picture layout - text wrapping
                    headerPicture.TextWrappingStyle = TextWrappingStyle.Behind

                    'header picture layout - position
                    headerPicture.HorizontalOrigin = HorizontalOrigin.Page
                    headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left
                    headerPicture.VerticalOrigin = VerticalOrigin.Page
                    headerPicture.VerticalAlignment = ShapeVerticalAlignment.Top
                End If

                'header text
                Dim text As TextRange = headerParagraph.AppendText(headerText)
                text.CharacterFormat.FontName = fontName
                text.CharacterFormat.FontSize = Convert.ToSingle(fontSize)
                text.CharacterFormat.TextColor = fontColor
                headerParagraph.Format.HorizontalAlignment = alignment

                'border
                headerParagraph.Format.Borders.Bottom.BorderType _
                    = DirectCast(BorderStyle.Hairline, Spire.Doc.Documents.BorderStyle)

                headerParagraph.Format.Borders.Bottom.Space = CSng(0.03)
            Next
        End Sub
        Private Function ConvertStrToColor(strColor As String) As Color
            If [String].IsNullOrWhiteSpace(strColor) Then
                Return Color.Empty
            Else
                Return ColorTranslator.FromHtml("#" & strColor)
            End If
        End Function
    End Class
End Namespace