Word Header and Footer in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to insert header and footer into a Word document.

private void button1_Click(object sender, EventArgs e)
{
    //Create word document
    Document document = new Document();

    InsertHeaderFooter(document);

    //Save doc file.
    document.SaveToFile("Sample.doc",FileFormat.Doc);

    //Launching the MS Word file.
    WordDocViewer("Sample.doc");


}

private void InsertHeaderFooter(Document document)
{
    Section section = document.AddSection();
    Paragraph paragraph = section.AddParagraph();
    paragraph.AppendText("The sample demonstrates how to insert a header and footer into a document.");
    paragraph.ApplyStyle(BuiltinStyle.Heading2); 
    
    paragraph = section.AddParagraph();
    paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");

    section.PageSetup.DifferentFirstPageHeaderFooter = true;

    paragraph = new Paragraph(document);
    paragraph.AppendText("Spire.Doc for .NET").CharacterFormat.FontSize = 15;
    paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
    section.HeadersFooters.FirstPageHeader.Paragraphs.Add(paragraph);

    paragraph = new Paragraph(document);
    paragraph.AppendText("e-iceblue company Ltd. 2002-2010 All rights reserverd").CharacterFormat.FontSize = 15;
    paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
    section.HeadersFooters.FirstPageFooter.Paragraphs.Add(paragraph);
}

private void WordDocViewer(string fileName)
{
    try
    {
        System.Diagnostics.Process.Start(fileName);
    }
    catch { }
}

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
	'Create word document
	Dim document_Renamed As New Document()

	InsertHeaderFooter(document_Renamed)

	'Save doc file.
	document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)

	'Launching the MS Word file.
	WordDocViewer("Sample.doc")


End Sub

Private Sub InsertHeaderFooter(ByVal document_Renamed As Document)
	Dim section_Renamed As Section = document_Renamed.AddSection()
	Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()
	paragraph_Renamed.AppendText("The sample demonstrates how to insert a header and footer into a document.")
	paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2)

	section_Renamed.PageSetup.DifferentFirstPageHeaderFooter = True

	paragraph_Renamed = New Paragraph(document_Renamed)
	paragraph_Renamed.AppendText("Spire.Doc for .NET").CharacterFormat.FontSize = 15
	paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
	section_Renamed.HeadersFooters.FirstPageHeader.Paragraphs.Add(paragraph_Renamed)

	paragraph_Renamed = New Paragraph(document_Renamed)
	paragraph_Renamed.AppendText("e-iceblue company Ltd. 2002-2010 All rights reserverd").CharacterFormat.FontSize = 15
	paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
	section_Renamed.HeadersFooters.FirstPageFooter.Paragraphs.Add(paragraph_Renamed)
End Sub

Private Sub WordDocViewer(ByVal fileName As String)
	Try
		Process.Start(fileName)
	Catch
	End Try
End Sub