Headers and footers are pieces of text, or graphics, that appear at the top and bottom of word document. This header and footer is shown commonly in all pages of document. We can add a page number to a header or footer.
We can use different headers and footers on different pages
We have versions of this representation in words ,using dropdown you can make a choice within the following
- Word Doc
- Word Docx
- Word ML
- RTF
- MS Text format
- E-book format
- DLS based xml file format
- HTML
After clicking a generate button will launch a file with above choosen version of word with sample header and footer.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;
namespace OfficeComponent.Samples
{
class HeaderAndFooterExample : WordExampleBase
{
public HeaderAndFooterExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public HeaderAndFooterExample(string commonDataPath, string outputDir, string xmlFile)
: base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
// Create a new instance of PdfDocument class.
WordDocument document = new WordDocument();
// Add a new section to the document.
Section firstSection = document.AddSection();
AddFirstPageHeaderFooter(firstSection);
AddOtherPagesHeaderFooter(firstSection);
string content;
// Load content from a file.
using (StreamReader reader = new StreamReader(CommonDataPath + "\\EULA.txt"))
{
content = reader.ReadToEnd();
}
// Add text to the document.
Paragraph paragraph = firstSection.AddParagraph();
paragraph.AppendText(content);
string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
// Save the document.
document.Save(fileName, SaveAsFormat);
// Close the document.
document.Close();
return fileName;
}
void AddFirstPageHeaderFooter(Section firstSection)
{
// Use different headers & footers
firstSection.PageSetup.DifferentFirstPage = true;
#region Add Header
// Add a table to the first page's header.
Table headerTable = firstSection.HeadersFooters.FirstPageHeader.AddTable();
// with single border style.
RowFormat rowFormat = new RowFormat();
rowFormat.Borders.BorderLineStyle = OfficeComponent.Word.BorderStyle.Single;
// The table has 1 row with 2 columns.
headerTable.ResetCells(1, 2, rowFormat, 250);
Paragraph paragraph = headerTable[0, 0].AddParagraph();
// Add a text to the left column.
TextRange text = paragraph.AppendText("OfficeComponent\n20496 Pso Del Prado, Suite 100\nWalnut, CA 91789\nUSA");
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.FontName = "Calibri";
// Add an image to the right column.
paragraph = headerTable[0, 1].AddParagraph();
paragraph.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath + "\\Logo.gif"));
paragraph.ParagraphFormat.HorizontalAlignment = OfficeComponent.Word.HorizontalAlignment.Right;
#endregion
#region Add Footer
// Add a table to the first page's footer.
Table footerTable = firstSection.HeadersFooters.FirstPageFooter.AddTable();
// with single border style.
rowFormat = new RowFormat();
rowFormat.Borders.BorderLineStyle = OfficeComponent.Word.BorderStyle.Single;
// The table has 1 row with 2 columns.
footerTable.ResetCells(1, 1, rowFormat, 500);
paragraph = footerTable[0, 0].AddParagraph();
// Add a text to the left column.
text = paragraph.AppendText("World Leading .NET Component Provider");
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.FontName = "Calibri";
#endregion
}
void AddOtherPagesHeaderFooter(Section firstSection)
{
#region Add Header
// Add a table to the first page's header.
Table headerTable = firstSection.HeadersFooters.Header.AddTable();
// with single border style.
RowFormat rowFormat = new RowFormat();
rowFormat.Borders.BorderLineStyle = OfficeComponent.Word.BorderStyle.Single;
// The table has 1 row with 2 columns.
headerTable.ResetCells(1, 2, rowFormat, 250);
Paragraph paragraph = headerTable[0, 0].AddParagraph();
// Add a text to the left column.
TextRange text = paragraph.AppendText("OfficeComponent Word for .NET");
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.FontName = "Calibri";
// Add an image to the right column.
paragraph = headerTable[0, 1].AddParagraph();
paragraph.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath + "\\OfficeComponentWord.gif"));
paragraph.ParagraphFormat.HorizontalAlignment = OfficeComponent.Word.HorizontalAlignment.Right;
#endregion
#region Add Footer
// Add a table to the first page's footer.
Table footerTable = firstSection.HeadersFooters.Footer.AddTable();
// with single border style.
rowFormat = new RowFormat();
rowFormat.Borders.BorderLineStyle = OfficeComponent.Word.BorderStyle.Single;
// The table has 1 row with 2 columns.
footerTable.ResetCells(1, 1, rowFormat, 500);
paragraph = footerTable[0, 0].AddParagraph();
// Add a text to the left column.
text = paragraph.AppendText("OfficeComponent Word supports WinForms, ASP.NET & ASP.NET AJAX, ASP.NET MVC, Silverlight, WPF");
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.FontName = "Calibri";
#endregion
}
}
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Word
Namespace OfficeComponent.Samples
Friend Class HeaderAndFooterExample
Inherits WordExampleBase
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
' Create a new instance of PdfDocument class.
Dim document As New WordDocument()
' Add a new section to the document.
Dim firstSection As Section = document.AddSection()
AddFirstPageHeaderFooter(firstSection)
AddOtherPagesHeaderFooter(firstSection)
Dim content As String
' Load content from a file.
Using reader As New StreamReader(CommonDataPath & "\EULA.txt")
content = reader.ReadToEnd()
End Using
' Add text to the document.
Dim paragraph As Paragraph = firstSection.AddParagraph()
paragraph.AppendText(content)
Dim fileName As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
' Save the document.
document.Save(fileName, SaveAsFormat)
' Close the document.
document.Close()
Return fileName
End Function
Private Sub AddFirstPageHeaderFooter(ByVal firstSection As Section)
' Use different headers & footers
firstSection.PageSetup.DifferentFirstPage = True
' #Region "Add Header"
' Add a table to the first page's header.
Dim headerTable As Table = firstSection.HeadersFooters.FirstPageHeader.AddTable()
' with single border style.
Dim rowFormat As New RowFormat()
rowFormat.Borders.BorderLineStyle = OfficeComponent.Word.BorderStyle.Single
' The table has 1 row with 2 columns.
headerTable.ResetCells(1, 2, rowFormat, 250)
Dim paragraph As Paragraph = headerTable(0, 0).AddParagraph()
' Add a text to the left column.
Dim text As TextRange = paragraph.AppendText("OfficeComponent" & vbLf & "20496 Pso Del Prado, Suite 100" & vbLf & "Walnut, CA 91789" & vbLf & "USA")
text.CharacterFormat.FontSize = 12
text.CharacterFormat.FontName = "Calibri"
' Add an image to the right column.
paragraph = headerTable(0, 1).AddParagraph()
paragraph.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath & "\Logo.gif"))
paragraph.ParagraphFormat.HorizontalAlignment = OfficeComponent.Word.HorizontalAlignment.Right
' #End Region
' #Region "Add Footer"
' Add a table to the first page's footer.
Dim footerTable As Table = firstSection.HeadersFooters.FirstPageFooter.AddTable()
' with single border style.
rowFormat = New RowFormat()
rowFormat.Borders.BorderLineStyle = OfficeComponent.Word.BorderStyle.Single
' The table has 1 row with 2 columns.
footerTable.ResetCells(1, 1, rowFormat, 500)
paragraph = footerTable(0, 0).AddParagraph()
' Add a text to the left column.
text = paragraph.AppendText("World Leading .NET Component Provider")
text.CharacterFormat.FontSize = 12
text.CharacterFormat.FontName = "Calibri"
' #End Region
End Sub
Private Sub AddOtherPagesHeaderFooter(ByVal firstSection As Section)
' #Region "Add Header"
' Add a table to the first page's header.
Dim headerTable As Table = firstSection.HeadersFooters.Header.AddTable()
' with single border style.
Dim rowFormat As New RowFormat()
rowFormat.Borders.BorderLineStyle = OfficeComponent.Word.BorderStyle.Single
' The table has 1 row with 2 columns.
headerTable.ResetCells(1, 2, rowFormat, 250)
Dim paragraph As Paragraph = headerTable(0, 0).AddParagraph()
' Add a text to the left column.
Dim text As TextRange = paragraph.AppendText("OfficeComponent Word for .NET")
text.CharacterFormat.FontSize = 12
text.CharacterFormat.FontName = "Calibri"
' Add an image to the right column.
paragraph = headerTable(0, 1).AddParagraph()
paragraph.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath & "\OfficeComponentWord.gif"))
paragraph.ParagraphFormat.HorizontalAlignment = OfficeComponent.Word.HorizontalAlignment.Right
' #End Region
' #Region "Add Footer"
' Add a table to the first page's footer.
Dim footerTable As Table = firstSection.HeadersFooters.Footer.AddTable()
' with single border style.
rowFormat = New RowFormat()
rowFormat.Borders.BorderLineStyle = OfficeComponent.Word.BorderStyle.Single
' The table has 1 row with 2 columns.
footerTable.ResetCells(1, 1, rowFormat, 500)
paragraph = footerTable(0, 0).AddParagraph()
' Add a text to the left column.
text = paragraph.AppendText("OfficeComponent Word supports WinForms, ASP.NET & ASP.NET AJAX, ASP.NET MVC, Silverlight, WPF")
text.CharacterFormat.FontSize = 12
text.CharacterFormat.FontName = "Calibri"
' #End Region
End Sub
End Class
End Namespace