Header and Footer

Every document can contain headers and / or footers which can be defined on the first page, even pages and all other pages.

Following sample shows how to add headers and footers to the first page, pages with even page numbers and all other pages.

Screenshot

Header and Footer Screenshot

See the full code below, use RUN EXAMPLE to execute.


1using System;
2using System.IO;
3using System.Linq;
4using GemBox.Document;
5using GemBox.Document.Drawing;
6
7class Sample
8{
9    [STAThread]
10    static void Main(string[] args)
11    {
12        // If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
14
15        DocumentModel document = new DocumentModel();
16
17        Section section = new Section(document,
18            new Paragraph(document,
19                new Run(document, "First page"),
20                new SpecialCharacter(document, SpecialCharacterType.PageBreak)),
21            new Paragraph(document,
22                new Run(document, "Even page"),
23                new SpecialCharacter(document, SpecialCharacterType.PageBreak)),
24            new Paragraph(document,
25                new Run(document, "Odd page"),
26                new SpecialCharacter(document, SpecialCharacterType.PageBreak)),
27            new Paragraph(document, "Even page"));
28
29        section.HeadersFooters.Add(
30            new HeaderFooter(document, HeaderFooterType.HeaderFirst,
31                new Paragraph(document,
32                    new Run(document, "First Header"))));
33
34        // Add page number.
35        section.HeadersFooters.Add(
36            new HeaderFooter(document, HeaderFooterType.FooterFirst,
37                new Paragraph(document,
38                    new Run(document, "First Footer")),
39                new Paragraph(document,
40                    new Field(document, FieldType.Page))
41                {
42                    ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Right }
43                }));
44
45        section.HeadersFooters.Add(
46            new HeaderFooter(document, HeaderFooterType.HeaderDefault,
47                new Paragraph(document,
48                    new Run(document, "Default Header"))));
49
50        section.HeadersFooters.Add(
51            new HeaderFooter(document, HeaderFooterType.FooterDefault,
52                new Paragraph(document,
53                    new Run(document, "Default Footer")),
54                 new Paragraph(document,
55                    new Field(document, FieldType.Page))
56                 {
57                     ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Right }
58                 }));
59
60        section.HeadersFooters.Add(
61            new HeaderFooter(document, HeaderFooterType.HeaderEven,
62                new Paragraph(document,
63                    new Run(document, "Even Header"))));
64
65        section.HeadersFooters.Add(
66            new HeaderFooter(document, HeaderFooterType.FooterEven,
67                new Paragraph(document,
68                    new Run(document, "Even Footer")),
69                new Paragraph(document,
70                    new Field(document, FieldType.Page))
71                {
72                    ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Right }
73                }));
74
75        document.Sections.Add(section);
76
77        document.Save("Header and Footer.docx");
78    }
79}
1Imports System
2Imports System.IO
3Imports System.Linq
4Imports GemBox.Document
5Imports GemBox.Document.Drawing
6
7Module Samples
8
9    Sub Main()
10
11        ' If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
13
14        Dim document As DocumentModel = New DocumentModel
15
16        Dim section As New Section(document,
17                New Paragraph(document,
18                    New Run(document, "First page"),
19                    New SpecialCharacter(document, SpecialCharacterType.PageBreak)),
20                New Paragraph(document,
21                    New Run(document, "Even page"),
22                    New SpecialCharacter(document, SpecialCharacterType.PageBreak)),
23                New Paragraph(document,
24                    New Run(document, "Odd page"),
25                    New SpecialCharacter(document, SpecialCharacterType.PageBreak)),
26                New Paragraph(document, "Even page"))
27
28        section.HeadersFooters.Add(
29            New HeaderFooter(document, HeaderFooterType.HeaderFirst,
30                New Paragraph(document,
31                    New Run(document, "First Header"))))
32
33        ' Add page number.
34        section.HeadersFooters.Add(
35            New HeaderFooter(document, HeaderFooterType.FooterFirst,
36                New Paragraph(document,
37                    New Run(document, "First Footer")),
38                New Paragraph(document,
39                    New Field(document, FieldType.Page)) With {
40                .ParagraphFormat = New ParagraphFormat() With {
41                    .Alignment = HorizontalAlignment.Right
42         }}))
43
44        section.HeadersFooters.Add(
45            New HeaderFooter(document, HeaderFooterType.HeaderDefault,
46                New Paragraph(document,
47                    New Run(document, "Default Header"))))
48
49        section.HeadersFooters.Add(
50            New HeaderFooter(document, HeaderFooterType.FooterDefault,
51                New Paragraph(document,
52                    New Run(document, "Default Footer")),
53                New Paragraph(document,
54                    New Field(document, FieldType.Page)) With {
55                .ParagraphFormat = New ParagraphFormat() With {
56                    .Alignment = HorizontalAlignment.Right
57         }}))
58
59        section.HeadersFooters.Add(
60            New HeaderFooter(document, HeaderFooterType.HeaderEven,
61                New Paragraph(document,
62                    New Run(document, "Even Header"))))
63
64        section.HeadersFooters.Add(
65            New HeaderFooter(document, HeaderFooterType.FooterEven,
66                New Paragraph(document,
67                    New Run(document, "Even Footer")),
68                New Paragraph(document,
69                    New Field(document, FieldType.Page)) With {
70                .ParagraphFormat = New ParagraphFormat() With {
71                    .Alignment = HorizontalAlignment.Right
72         }}))
73
74        document.Sections.Add(section)
75
76        document.Save("Header and Footer.docx")
77
78    End Sub
79
80End Module

Check next sample or find out more about GemBox.Document and GemBox Software.