Articles in this section
Category / Section

How to add different header and footer for last page alone in the Word document?

4 mins read

Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can add different header and footer for last page alone in the Word document in C#.

Steps to add different header and footer for last page alone in the Word document

  1. Create a new C# .NET Core console application project. Create .NET Core console application in Visual Studio in ASP.NET Core Word
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.Add DocIO.Net.Core NuGet packages of ASP.NET Core Word
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
  1. Use the following code example to add different header and footer for last page alone in the Word document.

C#

//Create a new Word document.
using (WordDocument document = new WordDocument())
{
    //Add the first section to the document.
    IWSection section = document.AddSection();
    //Add a paragraph to the section.
    IWParagraph paragraph = section.AddParagraph();
    string paraText = "AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.";
    //Append some text to the first page in document.
    paragraph.AppendText("\r[ First Page ] \r\r" + paraText);
    //Insert default header and footer to the section.
    paragraph = section.HeadersFooters.Header.AddParagraph();
    paragraph.AppendText("[ Header ]");
    paragraph = section.HeadersFooters.Footer.AddParagraph();
    paragraph.AppendText("[ Footer ]");
    //Add the second section to the document.
    section = document.AddSection();
    //Append some text to the second page in document.
    paragraph = section.AddParagraph();
    paragraph.AppendText("\r[ Second Page ] \r\r" + paraText);
    //Add the third section to the document.
    section = document.AddSection();
    //Append some text to the third page in document.
    paragraph = section.AddParagraph();
    paragraph.AppendText("\r[ Third Page ] \r\r" + paraText);
    //Insert default header and footer to the section.
    paragraph = section.HeadersFooters.Header.AddParagraph();
    paragraph.AppendText("[ Third Page Header ]");
    paragraph = section.HeadersFooters.Footer.AddParagraph();
    paragraph.AppendText("[ Third Page Footer ]");
    //Create a file stream.
    using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite))
    {
        //Save the Word document to the file stream.
        document.Save(outputFileStream, FormatType.Docx);
    }
}

A complete working sample to add different header and footer for last page alone in the Word document in C# can be downloaded from GitHub.

By executing the program, you will get the output document as follows.

Output document generated in ASP.NET Core Word

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail mergemerge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied