Create different headers/footers for odd and even pages

Headers and footers are widely used to show addition information such as chapter name, page numbers to keep the document organized. By default, MS Word sets the same headers and footers on each page, but sometimes we need to create different headers or footers for odd and even pages. This article is going to introduce the method to set different odd and even header/footer using Spire.Doc in C#.

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: Create a new document and load from file.

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

Step 2: Add a section and set the property true.

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

Step 3: Create odd and even footer, odd and even header, and set their format.

//add EvenFooter
Paragraph P1 = section.HeadersFooters.EvenFooter.AddParagraph();
TextRange EF = P1.AppendText("Even Footer Demo from E-iceblue Using Spire.Doc");
EF.CharacterFormat.FontName = "Calibri";
EF.CharacterFormat.FontSize = 20;
EF.CharacterFormat.TextColor = Color.Green;
EF.CharacterFormat.Bold = true;
P1.Format.HorizontalAlignment = HorizontalAlignment.Center;
           
//add OddFooter
Paragraph P2 = section.HeadersFooters.OddFooter.AddParagraph();
TextRange OF = P2.AppendText("Odd Footer Demo");
P2.Format.HorizontalAlignment = HorizontalAlignment.Center;
OF.CharacterFormat.FontName = "Calibri";
OF.CharacterFormat.FontSize = 20;
OF.CharacterFormat.Bold = true;
OF.CharacterFormat.TextColor = Color.Blue;

//add OddHeader
Paragraph P3 = section.HeadersFooters.OddHeader.AddParagraph();
TextRange OH = P3.AppendText("Odd Header Demo");
P3.Format.HorizontalAlignment = HorizontalAlignment.Center;
OH.CharacterFormat.FontName = "Calibri";
OH.CharacterFormat.FontSize = 20;
OH.CharacterFormat.Bold = true;
OH.CharacterFormat.TextColor = Color.Blue;

//add EvenHeader
Paragraph P4 = section.HeadersFooters.EvenHeader.AddParagraph();
TextRange EH = P4.AppendText("Even Header Demo from E-iceblue Using Spire.Doc");
P4.Format.HorizontalAlignment = HorizontalAlignment.Center;
EH.CharacterFormat.FontName = "Calibri";
EH.CharacterFormat.FontSize = 20;
EH.CharacterFormat.Bold = true;
EH.CharacterFormat.TextColor = Color.Green;

Step 4: Save the document and launch to see effects.

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

Effects:

How to create different headers or footers for odd and even pages

Full code:

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("T1.docx");
            Section section = document.Sections[0];

            section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true;

            Paragraph P1 = section.HeadersFooters.EvenFooter.AddParagraph();
            TextRange EF = P1.AppendText("Even Footer Demo from E-iceblue Using Spire.Doc");
            EF.CharacterFormat.FontName = "Calibri";
            EF.CharacterFormat.FontSize = 20;
            EF.CharacterFormat.TextColor = Color.Green;
            EF.CharacterFormat.Bold = true;
            P1.Format.HorizontalAlignment = HorizontalAlignment.Center;
           

            Paragraph P2 = section.HeadersFooters.OddFooter.AddParagraph();
            TextRange OF = P2.AppendText("Odd Footer Demo");
            P2.Format.HorizontalAlignment = HorizontalAlignment.Center;
            OF.CharacterFormat.FontName = "Calibri";
            OF.CharacterFormat.FontSize = 20;
            OF.CharacterFormat.Bold = true;
            OF.CharacterFormat.TextColor = Color.Blue;

            Paragraph P3 = section.HeadersFooters.OddHeader.AddParagraph();
            TextRange OH = P3.AppendText("Odd Header Demo");
            P3.Format.HorizontalAlignment = HorizontalAlignment.Center;
            OH.CharacterFormat.FontName = "Calibri";
            OH.CharacterFormat.FontSize = 20;
            OH.CharacterFormat.Bold = true;
            OH.CharacterFormat.TextColor = Color.Blue;

            Paragraph P4 = section.HeadersFooters.EvenHeader.AddParagraph();
            TextRange EH = P4.AppendText("Even Header Demo from E-iceblue Using Spire.Doc");
            P4.Format.HorizontalAlignment = HorizontalAlignment.Center;
            EH.CharacterFormat.FontName = "Calibri";
            EH.CharacterFormat.FontSize = 20;
            EH.CharacterFormat.Bold = true;
            EH.CharacterFormat.TextColor = Color.Green;

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