Adding, modifying, and removing footers in a PowerPoint document is crucial for enhancing the professionalism and readability of a presentation. By adding footers, you can include key information such as presentation titles, authors, dates, or page numbers, which helps the audience better understand the content. Modifying footers allows you to update information, making it more attractive and practical. Additionally, removing footers is also necessary, especially in certain situations such as when specific information is not required to be displayed at the bottom of each page for a particular occasion. In this article, you will learn how to add, modify, or remove footers in PowerPoint documents in C# using Spire.Presentation for .NET.

Install Spire.Presentation for .NET

To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Presentation

C# Add Footers in PowerPoint Documents

Spire.Presentation enables the addition of footer, slide number, and date placeholders at the bottom of PowerPoint document pages to uniformly add the same footer content across all pages. Here are the detailed steps:

  • Create a Presentation object.
  • Load a PowerPoint document using the lisentation.LoadFromFile() method.
  • Set Presentation.FooterVisible = true to make the footer visible and set the footer text.
  • Set Presentation.SlideNumberVisible = true to make slide numbers visible, then iterate through each slide, check for the existence of a slide number placeholder, and modify the text to "Slide X" format if found.
  • Set Presentation.DateTimeVisible = true to make date and time visible.
  • Use the Presentation.SetDateTime() method to set the date format.
  • Save the document using the Presentation.SaveToFile() method.
  • C#
using Spire.Presentation;

namespace SpirePresentationDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a Presentation object
            Presentation presentation = new Presentation();

            // Load the presentation from a file
            presentation.LoadFromFile("Sample1.pptx");

            // Set the footer visible
            presentation.FooterVisible = true;

            // Set the footer text to "Spire.Presentation"
            presentation.SetFooterText("Spire.Presentation");

            // Set slide number visible
            presentation.SlideNumberVisible = true;

            // Iterate through each slide in the presentation
            foreach (ISlide slide in presentation.Slides)
            {
                foreach (IShape shape in slide.Shapes)
                {
                    if (shape.Placeholder != null)
                    {
                        // If it is a slide number placeholder
                        if (shape.Placeholder.Type.Equals(PlaceholderType.SlideNumber))
                        {
                            TextParagraph textParagraph = (shape as IAutoShape).TextFrame.TextRange.Paragraph;
                            String text = textParagraph.Text;

                            // Modify the slide number text to "Slide X"
                            textParagraph.Text = "Slide " + text;
                        }
                    }
                }
            }

            // Set date time visible
            presentation.DateTimeVisible = true;

            // Set date time format
            presentation.SetDateTime(DateTime.Now, "MM/dd/yyyy");

            // Save the modified presentation to a file
            presentation.SaveToFile("AddFooter.pptx", FileFormat.Pptx2016);

            // Dispose of the Presentation object resources
            presentation.Dispose();
        }
    }
}

C#: Add, Modify, or Remove Footers in PowerPoint Documents

C# Modify Footers in PowerPoint Documents

To modify footers in a PowerPoint document, you first need to individually inspect the shapes on each slide, identify footer placeholders, page number placeholders, etc., and then set specific content and formats for each type of placeholder. Here are the detailed steps:

  • Create a Presentation object.
  • Load a PowerPoint document using the Presentation.LoadFromFile() method.
  • Use the Presentation.Slides[index] property to retrieve a slide.
  • Use a for loop to iterate through the shapes on the slide, individually checking each shape to determine if it is a placeholder for a footer, page number, etc., and then modify its content or format accordingly.
  • Save the document using the Presentation.SaveToFile() method.
  • C#
using Spire.Presentation;

namespace SpirePresentationDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a Presentation object
            Presentation presentation = new Presentation();

            // Load the presentation from a file
            presentation.LoadFromFile("Sample2.pptx");

            // Get the first slide
            ISlide slide = presentation.Slides[0];

            // Iterate through shapes in the slide
            for (int i = 0; i < slide.Shapes.Count; i++)
            {
                // Check if the shape is a placeholder
                if (slide.Shapes[i].Placeholder != null)
                {
                    // Get the placeholder type
                    PlaceholderType type = slide.Shapes[i].Placeholder.Type;

                    // If it is a footer placeholder
                    if (type == PlaceholderType.Footer)
                    {
                        // Convert the shape to IAutoShape type
                        IAutoShape autoShape = (IAutoShape)slide.Shapes[i];

                        // Set the text content to "E-ICEBLUE"
                        autoShape.TextFrame.Text = "E-ICEBLUE";

                        // Modify the text font
                        ChangeFont1(autoShape.TextFrame.Paragraphs[0]);
                    }
                    // If it is a slide number placeholder
                    if (type == PlaceholderType.SlideNumber)
                    {
                        // Convert the shape to IAutoShape type
                        IAutoShape autoShape = (IAutoShape)slide.Shapes[i];

                        // Modify the text font
                        ChangeFont1(autoShape.TextFrame.Paragraphs[0]);
                    }
                }
            }

            // Save the modified presentation to a file
            presentation.SaveToFile("ModifyFooter.pptx", FileFormat.Pptx2016);

            // Dispose of the Presentation object resources
            presentation.Dispose();
        }
       static void ChangeFont1(TextParagraph paragraph)
        {
            // Iterate through each text range in the paragraph
            foreach (TextRange textRange in paragraph.TextRanges)
            {
                // Set the text style to italic
                textRange.IsItalic = TriState.True;

                // Set the text font
                textRange.EastAsianFont = new TextFont("Times New Roman");

                // Set the text font size to 34
                textRange.FontHeight = 34;

                // Set the text color
                textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                textRange.Fill.SolidColor.Color = System.Drawing.Color.LightSkyBlue;
            }
        }
   }  
}

C#: Add, Modify, or Remove Footers in PowerPoint Documents

C# Remove Footers in PowerPoint Documents

To remove footers from a PowerPoint document, you first need to locate footer placeholders, page number placeholders, date placeholders, etc., within the slides, and then remove them from the collection of shapes on the slide. Here are the detailed steps:

  • Create a Presentation object.
  • Load a PowerPoint document using the Presentation.LoadFromFile() method.
  • Use the Presentation.Slides[index] property to retrieve a slide.
  • Use a for loop to iterate through the shapes on the slide, check if it is a placeholder, and if it is a footer placeholder, page number placeholder, date placeholder, remove it from the slide.
  • Save the document using the Presentation.SaveToFile() method.
  • C#
using Spire.Presentation;

namespace SpirePresentationDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a Presentation object
            Presentation presentation = new Presentation();

            // Load the presentation from a file
            presentation.LoadFromFile("Sample2.pptx");

            // Get the first slide
            ISlide slide = presentation.Slides[0];

            // Iterate through shapes in the slide in reverse order
            for (int i = slide.Shapes.Count - 1; i >= 0; i--)
            {
                // Check if the shape is a placeholder
                if (slide.Shapes[i].Placeholder != null)
                {
                    // Get the placeholder type
                    PlaceholderType type = slide.Shapes[i].Placeholder.Type;

                    // If it is a footer placeholder, slide number placeholder, or date placeholder
                    if (type == PlaceholderType.Footer || type == PlaceholderType.SlideNumber || type == PlaceholderType.DateAndTime)
                    {
                        // Remove it from the slide
                        slide.Shapes.RemoveAt(i);
                    }
                }
            }

            // Save the modified presentation to a file
            presentation.SaveToFile("RemoveFooter.pptx", FileFormat.Pptx2016);

            // Dispose of the Presentation object resources
            presentation.Dispose();
        }
    }
}

C#: Add, Modify, or Remove Footers in PowerPoint Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Header and Footer

The height of headers and footers can be adjusted by using the HeaderDistance and the FooterDistance properties. The detail steps of how to adjust the height of headers and footers in a word document using Spire.Doc are shown below.

Detail steps:

Step 1: Instantiate a Document object and load the word document.

Document doc = new Document();
doc.LoadFromFile("Headers and Footers.docx");

Step 2: Get the first section.

Section section = doc.Sections[0];

Step 3: Adjust the height of headers and footers in the section.

section.PageSetup.HeaderDistance = 100;
section.PageSetup.FooterDistance = 100;

Step 4: Save the file.

doc.SaveToFile("Output.docx", FileFormat.Docx2013);

Screenshot:

Header:

Adjust the Height of Headers and Footers in a Word document in C#

Footer:

Adjust the Height of Headers and Footers in a Word document in C#

Full code:

//Instantiate a Document object
Document doc = new Document();
//Load the word document
doc.LoadFromFile("Headers and Footers.docx");

//Get the first section
Section section = doc.Sections[0];

//Adjust the height of headers in the section
section.PageSetup.HeaderDistance = 100;

//Adjust the height of footers in the section
section.PageSetup.FooterDistance = 100;

//Save the document
doc.SaveToFile("Output.docx", FileFormat.Docx2013);
Published in Header and Footer
Thursday, 24 May 2018 08:41

Remove footers from word document in C#

There are up to three kinds of headers and footers on a word document (for first, even and odd pages) and Spire.Doc supports to insert footer and header to the word document in C#. In the following example, we will load a document with headers and footers, and we will remove all footers from all sections, but leaves headers.

Step 1: Create a new instance of word document and load the sample document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 2: Get the first section from file.

Section section = doc.Sections[0];

Step 3: Traverse the word document and clear all footers in different type.

foreach (Paragraph para in section.Paragraphs)
{
    foreach (DocumentObject obj in para.ChildObjects)
    {
        HeaderFooter footer;
        footer = section.HeadersFooters[HeaderFooterType.FooterFirstPage];
        if (footer != null)
            footer.ChildObjects.Clear();

        footer = section.HeadersFooters[HeaderFooterType.FooterOdd];
        if (footer != null)
            footer.ChildObjects.Clear();

        footer = section.HeadersFooters[HeaderFooterType.FooterEven];
        if (footer != null)
            footer.ChildObjects.Clear();
                          
    }

Step 4: Save the document to file.

doc.SaveToFile("Result.docx", FileFormat.Docx2013);

Full codes of removing footers but keeping the header on word document:

static void Main(string[] args)
{

    Document doc = new Document();
    doc.LoadFromFile("Sample.docx");

    Section section = doc.Sections[0];

    foreach (Paragraph para in section.Paragraphs)
    {
        foreach (DocumentObject obj in para.ChildObjects)
        {
            HeaderFooter footer;
            footer = section.HeadersFooters[HeaderFooterType.FooterFirstPage];
            if (footer != null)
                footer.ChildObjects.Clear();

            footer = section.HeadersFooters[HeaderFooterType.FooterOdd];
            if (footer != null)
                footer.ChildObjects.Clear();

            footer = section.HeadersFooters[HeaderFooterType.FooterEven];
            if (footer != null)
                footer.ChildObjects.Clear();
                              
        }
        
        doc.SaveToFile("Result.docx", FileFormat.Docx2013);


    }
Published in Header and Footer

With the help of Spire.Doc, we can easily add and remove header on the word documents in C#. This article we will demonstrate how to lock down the header information from editing. We will divide it into two parts for the demo. Once is for locking the header information on the existing word document with header and the other is on the new creating word document.

How to lock the header information on the existing word document.

//Load the sample document with header
Document doc = new Document();
doc.LoadFromFile("sample.docx");

//Get the first section from the word document
Section section = doc.Sections[0];

//Protect the document and set the ProtectionType as AllowOnlyFormFields
doc.Protect(ProtectionType.AllowOnlyFormFields, "123");

//Set the ProtectForm as false to unprotect the section
section.ProtectForm = false;

//Save the document to file
doc.SaveToFile("Result.docx", FileFormat.Docx2013);

Effective screenshot of the header has been locked and the other area can be edited:

How to lock the header from editing on word document in C#

How to lock the header information for the new word document.

//Create a new instance of word document
Document doc = new Document();

//Add a section to the word document
Section section = doc.AddSection();

//Add header information to the section
HeaderFooter header = section.HeadersFooters.Header;
Paragraph HParagraph = header.AddParagraph();
TextRange HText = HParagraph.AppendText("Protect header");

//Add a paragraph to the section
Paragraph Para = section.AddParagraph();
Para.AppendText("Demo of how to lock the header information by Spire.Doc ");

//Set the ProtectionType as AllowOnlyFormFields and then unprotect the section
doc.Protect(ProtectionType.AllowOnlyFormFields, "123");
section.ProtectForm = false;

//Save the document to file
doc.SaveToFile("Result.docx", FileFormat.Docx2013);

How to lock the header from editing on word document in C#

Published in Header and Footer

When you create several Word documents that are closely related, you may want the header or footer of one document to be used as the header or footer of other documents. For example, you're creating internal documents with your company logo or name or other material been placed in header, you only have to create the header once and copy the header to other places.

In this article, I'll introduce you a simple and efficient solution to copy the entire header (including text and graphic) from one Word document and insert it to another.

Source Document:

Copy Header/Footer between Word Documents in C#, VB.NET

Detail Steps:

Step 1: Create a new instance of Document class and load the source file.

Document doc1 = new Document();
doc1.LoadFromFile("test1.docx");

Step 2: Get the header section from the source document.

HeaderFooter header = doc1.Sections[0].HeadersFooters.Header;

Step 3: Initialize a new instance of Document and load another file that you want to insert header.

Document doc2 = new Document("test2.docx");

Step 4: Call DocuentObject.Clone() method to copy each object in the header of source file, then call DocumentObjectCollection.Add() method to insert copied object into the header of destination file.

foreach (Section section in doc2.Sections)
{
    foreach (DocumentObject obj in header.ChildObjects)
    {
        section.HeadersFooters.Header.ChildObjects.Add(obj.Clone());
    }
}

Step 5: Save the changes and launch the file.

doc2.SaveToFile("test2.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("test2.docx");

Destination Document:

Copy Header/Footer between Word Documents in C#, VB.NET

Full Code:

[C#]
Document doc1 = new Document();
doc1.LoadFromFile("test1.docx");
HeaderFooter header = doc1.Sections[0].HeadersFooters.Header;
Document doc2 = new Document("test2.docx");
foreach (Section section in doc2.Sections)
{
    foreach (DocumentObject obj in header.ChildObjects)
    {
        section.HeadersFooters.Header.ChildObjects.Add(obj.Clone());
    }
}
doc2.SaveToFile("test2.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("test2.docx");
[VB.NET]
Dim doc1 As New Document()
doc1.LoadFromFile("test1.docx")
Dim header As HeaderFooter = doc1.Sections(0).HeadersFooters.Header
Dim doc2 As New Document("test2.docx")
For Each section As Section In doc2.Sections
	For Each obj As DocumentObject In header.ChildObjects
		section.HeadersFooters.Header.ChildObjects.Add(obj.Clone())
	Next
Next
doc2.SaveToFile("test2.docx", FileFormat.Docx2013)
System.Diagnostics.Process.Start("test2.docx")
Published in Header and Footer

In MS Word page border options, there are checkboxes to choose whether page border surrounds header/footer or not. This feature is very important for which can be used to manage the positional relation between page border and header/footer. Spire.Doc supports to set the two frequently-used features: page border and header/footer, and it supports to manage their spatial relations, too. This article is going to introduce the method to set whether page border surrounds header/footer or not.

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

Step 1: Create a Word document and add a section.

            Document document = new Document();
            Section section = document.AddSection();

Step 2: Add a sample page border to the document using Spire.Doc library.

            section.PageSetup.Borders.BorderType = BorderStyle.Wave;
            section.PageSetup.Borders.Color = Color.Green;
            section.PageSetup.Borders.Left.Space = 20;
            section.PageSetup.Borders.Right.Space = 20;

Step 3: Add sample header and footer to the document using Spire.Doc library.

            //add a header and set its format
            Paragraph paragraph1 = section.HeadersFooters.Header.AddParagraph();
            paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;
            TextRange headerText = paragraph1.AppendText("Header isn't included in page border");
            headerText.CharacterFormat.FontName = "Calibri";
            headerText.CharacterFormat.FontSize = 20;
            headerText.CharacterFormat.Bold = true;

            //add a footer and set its format
            Paragraph paragraph2 = section.HeadersFooters.Footer.AddParagraph();
            paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left;
            TextRange footerText = paragraph2.AppendText("Footer is included in page border");
            footerText.CharacterFormat.FontName = "Calibri";
            footerText.CharacterFormat.FontSize = 20;
            footerText.CharacterFormat.Bold = true;

Step 4: Set the header not included in the page border while the footer included.

            section.PageSetup.PageBorderIncludeHeader = false;
            section.PageSetup.HeaderDistance = 40;
            section.PageSetup.PageBorderIncludeFooter = true;
            section.PageSetup.FooterDistance = 40;

Step 5: Save the document and launch to see effect.

            document.SaveToFile("result.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result.docx"); 

Effects:

How to set whether page border surrounds header/footer or not

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();
            Section section = document.AddSection();

            section.PageSetup.Borders.BorderType = BorderStyle.Wave;
            section.PageSetup.Borders.Color = Color.Green;
            section.PageSetup.Borders.Left.Space = 20;
            section.PageSetup.Borders.Right.Space = 20;
            
            Paragraph paragraph1 = section.HeadersFooters.Header.AddParagraph();
            paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;
            TextRange headerText = paragraph1.AppendText("Header isn't included in page border");
            headerText.CharacterFormat.FontName = "Calibri";
            headerText.CharacterFormat.FontSize = 20;
            headerText.CharacterFormat.Bold = true;

            Paragraph paragraph2 = section.HeadersFooters.Footer.AddParagraph();
            paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left;
            TextRange footerText = paragraph2.AppendText("Footer is included in page border");
            footerText.CharacterFormat.FontName = "Calibri";
            footerText.CharacterFormat.FontSize = 20;
            footerText.CharacterFormat.Bold = true;

            section.PageSetup.PageBorderIncludeHeader = false;
            section.PageSetup.HeaderDistance = 40;
            section.PageSetup.PageBorderIncludeFooter = true;
            section.PageSetup.FooterDistance = 40;

            document.SaveToFile("result.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result.docx"); 
        }
    }
}
Published in 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");
        }
    }
}
Published in Header and Footer

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");
        }
    }
}
Published in Header and Footer

Page number is one of the formats of header and footer. It gives the total pages of a document and it is convenient for readers to index. Spire.Doc supports to add text and image header and footers. This tutorial will guide you how to add the page number on the header/footer of a document in C#.

Spire.Doc offers HeaderFooter class to set header or footer for word document. Then, invoke header/footer.AddParagraph() method to add header/footer paragraph and add contents by invoking paragraph.AppendText method. Here comes to the code snippet of how to add the page number for word documents.

Step 1: Create a new document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 2: Create footer for the first section and add a paragraph for the footer.

HeaderFooter footer = doc.Sections[0].HeadersFooters.Footer;
Paragraph footerParagraph = footer.AddParagraph();

Step 3: Add the page number to the footer and set the alignment for the paragraph.

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

Step 4: Save the document to file.

doc.SaveToFile("Add.docx", FileFormat.Docx);

Effective screenshot:

How to add the page number on the header/footer of a document

Full codes:

namespace AddPagenumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx");
            HeaderFooter footer = doc.Sections[0].HeadersFooters.Footer;
            Paragraph footerParagraph = footer.AddParagraph();
            footerParagraph.AppendField("page number", FieldType.FieldPage);
            footerParagraph.AppendText(" of ");
            footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
            footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
            doc.SaveToFile("Add.docx", FileFormat.Docx);

        }
    }
}
Published in Header and Footer
Tuesday, 21 October 2014 09:04

Remove header from the word document in C#

Spire.Doc for .NET enables developers to add, modify and remove text and Image header for word documents easily. We have already shown you how to insert header for all the pages and only add header for the first page in word document in C#. This article will focus on demonstrate how to remove the header for all the pages in word document and only remove the header for the first page in C#.

In the example, we will load a word document with headers. And then we will show you how to remove the header only from the first page and all the pages independently.

Check the word document with headers:

How to remove header from the word document in C#

Step 1: Create a new document and load from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 2: Get the first section of document.

Section section = doc.Sections[0];

Step 3: Remove the header only from the first page of word document.

//This is necessary
section.PageSetup.DifferentFirstPageHeaderFooter = true;
section.HeadersFooters.FirstPageHeader.ChildObjects.Clear();

Step 4: Remove the header for all the pages.

section.HeadersFooters.Header.ChildObjects.Clear();

Step 5: Save the document to file.

doc.SaveToFile("output.docx", FileFormat.Docx);

Effective screenshot of the removal of the header:

How to remove header from the word document in C#

Full codes:

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

namespace RemoveHeader
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Blues.docx");
            Section section = doc.Sections[0];
            //This is necessary
            section.PageSetup.DifferentFirstPageHeaderFooter = true;
            section.HeadersFooters.FirstPageHeader.ChildObjects.Clear();
            //section.HeadersFooters.Header.ChildObjects.Clear();
            doc.SaveToFile("output.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("output.docx");
        }
    }
}
Published in Header and Footer
Page 1 of 2