Add different first page header and footer

In the MS Word Header & Footer Tools options, we could choose "Different First Page" and "Different odd and even pages". The article "How to create different headers/footers for odd and even pages" introduces the method to set different odd and even pages using Spire.Doc. Spire.DOC also provides an easy and quick method to add different first page header & footer. This article is going to introduce the method to add different first page header & footer.

FYI, if you only need the first page header and footer, please just set the first page header & footer and leave the rest alone. In this way, your Word document will only have header & footer in the first page, which provides a simpler way to add a header only into the first page of a document than the method mentioned in the article "How to add a header only into the first page of a document".

Note: before start, please download the latest version of Spire.Doc and add Spire.Doc .dll in the bin folder as the reference of Visual Studio.

Step 1: Load the sample document that only contains text.

Document document = new Document();
document.LoadFromFile("T.docx");

Step 2: Get the section and set the property true.

Section section = document.Sections[0];
section.PageSetup.DifferentFirstPageHeaderFooter = true;

Step 3: Set the first page header. Here we append a picture as the header.

Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;           
DocPicture headerimage = paragraph1.AppendPicture(Image.FromFile("2.bmp"));

Step 4: Set the first page footer.

Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange FF = paragraph2.AppendText("First Page Footer");
FF.CharacterFormat.FontSize = 20;

Step 5: Set the other header & footer. If you only need the first page header & footer, don't set this.

Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange NH = paragraph3.AppendText("If you only need first page header, don't set this.");
NH.CharacterFormat.FontSize = 20;

Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange NF = paragraph4.AppendText("If you only need first page footer, don't set this.");
NF.CharacterFormat.FontSize = 20;

Step 6: save the document and launch to see effects.

document.SaveToFile("R.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("R.docx");

Effects:

How to add different first page header & footer

How to add different first page header & footer

Full codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace Mirror_Margin
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("T.docx");

            Section section = document.Sections[0];
            section.PageSetup.DifferentFirstPageHeaderFooter = true;

            Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
            paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;           
            DocPicture headerimage = paragraph1.AppendPicture(Image.FromFile("2.bmp"));

            Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
            paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;            
            TextRange FF = paragraph2.AppendText("First Page Footer");
            FF.CharacterFormat.FontSize = 20;

            Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
            paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange NH = paragraph3.AppendText("If you only need first page header, don't set this.");
            NH.CharacterFormat.FontSize = 20;

            Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
            paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange NF = paragraph4.AppendText("If you only need first page footer, don't set this.");
            NF.CharacterFormat.FontSize = 20;
           
            document.SaveToFile("R.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("R.docx");
        }
    }
}