How to add different header and footer for last page alone in the Word document?
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
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
- 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.
Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge 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.
Conclusion
I hope you enjoyed learning about how to add different header and footer for last page alone in the Word document.
You can refer to our .NET Word Library feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!