Spire.Doc for .NET (337)
Children categories
Word header and footer presents additional information of Word document, which can be text, image or page number. This guide focuses on introducing how to insert image header and footer for Word document in C# and VB.NET.
Header/Footer plays an important role in Word document, which uses text, image or page number to demonstrate some additional information about this document. The information can be company name, logo, author name, document title etc. This guide will demonstrate detailed process to insert image header/footer in Word with C# and VB.NET via Spire.Doc for .NET. The following screenshot displays Word image header/footer result after programming.

Spire.Doc for .NET provides a HeaderFooter. class to enable developers to generate a new header or footer. Firstly, initialize a header instance of HeaderFooter class and then invoke AddParagraph() method to add a paragraph body for this header/footer instance. Next, invoke Paragraph.AppendPicture(Image image) method to append a picture for header/footer paragraph. If you want to add text for paragraph as well, please invoke Paragraph.AppendText(string text) method. Also, you can set format for header/footer paragraph, appended image and text to have a better layout. Code as following:
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace ImageHeaderFooter
{
class Program
{
static void Main(string[] args)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\Spire.Doc for .NET.docx");
//Initialize a Header Instance
HeaderFooter header = document.Sections[0].HeadersFooters.Header;
//Add Header Paragraph and Format
Paragraph paragraph = header.AddParagraph();
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
//Append Picture for Header Paragraph and Format
DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"E:\Logo\doclog.png"));
headerimage.VerticalAlignment = ShapeVerticalAlignment.Bottom;
//Initialize a Footer Instance
HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;
//Add Footer Paragraph and Format
Paragraph paragraph2 = footer.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left;
//Append Picture and Text for Footer Paragraph
DocPicture footerimage = paragraph2.AppendPicture(Image.FromFile(@"E:\Logo\logo.jpeg"));
TextRange TR = paragraph2.AppendText("Copyright © 2013 e-iceblue. All Rights Reserved.");
TR.CharacterFormat.FontName = "Arial";
TR.CharacterFormat.FontSize = 10;
TR.CharacterFormat.TextColor = Color.Black;
//Save and Launch
document.SaveToFile("ImageHeaderFooter.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("ImageHeaderFooter.docx");
}
}
}
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace ImageHeaderFooter
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\Work\Documents\Spire.Doc for .NET.docx")
'Initialize a Header Instance
Dim header As HeaderFooter = document.Sections(0).HeadersFooters.Header
'Add Header Paragraph and Format
Dim paragraph As Paragraph = header.AddParagraph()
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right
'Append Picture for Header Paragraph and Format
Dim headerimage As DocPicture = paragraph.AppendPicture(Image.FromFile("E:\Logo\doclog.png"))
headerimage.VerticalAlignment = ShapeVerticalAlignment.Bottom
'Initialize a Footer Instance
Dim footer As HeaderFooter = document.Sections(0).HeadersFooters.Footer
'Add Footer Paragraph and Format
Dim paragraph2 As Paragraph = footer.AddParagraph()
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left
'Append Picture and Text for Footer Paragraph
Dim footerimage As DocPicture = paragraph2.AppendPicture(Image.FromFile("E:\Logo\logo.jpeg"))
Dim TR As TextRange = paragraph2.AppendText("Copyright © 2013 e-iceblue. All Rights Reserved.")
TR.CharacterFormat.FontName = "Arial"
TR.CharacterFormat.FontSize = 10
TR.CharacterFormat.TextColor = Color.Black
'Save and Launch
document.SaveToFile("ImageHeaderFooter.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("ImageHeaderFooter.docx")
End Sub
End Class
End Namespace
Spire.Doc, an easy-to-use component to perform Word tasks, allows developers to fast generate, write, edit and save Word (Word 97-2003, Word 2007, Word 2010) in C# and VB.NET for .NET, Silverlight and WPF.
Text alignment is a paragraph formatting attribute that determines the appearance of the text in a whole paragraph. There are four types of text alignments available in Microsoft Word: left-aligned, center-aligned, right-aligned, and justified. In this article, you will learn how to programmatically set different text alignments for paragraphs in a Word document using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc 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.Doc
Align Text in Word
The detailed steps are as follows:
- Create a Document instance.
- Load a sample Word document using Document.LoadFromFile() method.
- Get a specified section using Document.Sections[] property.
- Get a specified paragraph using Section.Paragraphs[] property.
- Get the paragraph format using Paragraph.Format property
- Set text alignment for the specified paragraph using ParagraphFormat.HorizontalAlignment property.
- Save the document to another file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
namespace AlignText
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document doc = new Document();
//Load a sample Word document
doc.LoadFromFile(@"D:\Files\sample.docx");
//Get the first section
Section section = doc.Sections[0];
//Get the first paragraph and make it center-aligned
Paragraph p = section.Paragraphs[0];
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Get the second paragraph and make it left-aligned
Paragraph p1 = section.Paragraphs[1];
p1.Format.HorizontalAlignment = HorizontalAlignment.Left;
//Get the third paragraph and make it right-aligned
Paragraph p2 = section.Paragraphs[2];
p2.Format.HorizontalAlignment = HorizontalAlignment.Right;
//Get the fourth paragraph and make it justified
Paragraph p3 = section.Paragraphs[3];
p3.Format.HorizontalAlignment = HorizontalAlignment.Justify;
//Save the document
doc.SaveToFile("WordAlignment.docx", FileFormat.Docx);
}
}
}

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.
When creating a new Word document, setting the appropriate fonts is crucial for establishing tone and enhancing readability. Fonts influence how your content is perceived, whether for formal reports, creative projects, or casual notes. By selecting the right typeface and size, you can ensure your document is not only visually appealing but also effective in communicating your message.
In this article, you will learn how to set fonts when creating a new Word document in C# using Spire.Doc for .NET.
- Set the Font for a Paragraph in C#
- Apply Different Fonts to Text in C#
- Use a Custom Font in a Word Document in C#
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc 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.Doc
Set the Font for a Paragraph in C#
Setting fonts is a fundamental task when working with Word documents in C#. Spire.Doc offers the ParagraphStyle class, which lets you specify font family, size, style, and text color. Once you've created a style, you can apply it to format a paragraph with the desired settings.
The detailed steps to set the font for a paragraph using C# are as follows:
- Create a Document object.
- Add a section and a paragraph to the document.
- Append text to the paragraph.
- Create a ParagraphStyle object.
- Get the CharacterFormat object through the ParagraphStyle.CharacterFormat property.
- Set the font style, name, size and text color using the properties under the CharacterFormat object.
- Add the style to the document using the Document.Styles.Add() method.
- Apply the defined style to the paragraph using the Paragraph.ApplyStyle() method.
- Save the document to a Word file.
Here is an example that demonstrates how to set the font for a paragraph when creating a new Word document in C#:
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace SetFontForParagraph
{
class Program
{
static void Main(string[] args)
{
// Create a Document object
Document doc = new Document();
// Add a section
Section section = doc.AddSection();
// Set the page margins
section.PageSetup.Margins.All = 40f;
// Add a paragraph
Paragraph paragraph = section.AddParagraph();
// Append text to the paragraph
paragraph.AppendText("Spire.Doc for .NET is a powerful library designed for developers " +
"working with document processing in the .NET environment. It enables users to create, " +
"read, modify, and convert various types of document formats, such as DOC, DOCX, PDF, " +
"and more, without the need for Microsoft Office installed on the machine.");
// Create a ParagraphStyle object
ParagraphStyle style = new ParagraphStyle(doc);
// Set the style name
style.Name = "myStyle";
// Set the font style, name, size and text color
style.CharacterFormat.Bold = false;
style.CharacterFormat.TextColor = Color.Black;
style.CharacterFormat.FontName = "Times New Roman";
style.CharacterFormat.FontSize = 14;
// Add the style the document
doc.Styles.Add(style);
// Apply the style to the paragraph
paragraph.ApplyStyle(style.Name);
// Set the horizontal alignment
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
// Save the document to a docx file
doc.SaveToFile("SetFont.docx", FileFormat.Docx2019);
// Dispose resources
doc.Dispose();
}
}
}

Apply Different Fonts to Text in C#
In some cases, you may need to apply various fonts to different segments of the same paragraph. Spire.Doc offers the TextRange class, which allows you to assign distinct styles to specific text ranges within a paragraph. To do this, ensure that the text requiring different styles is organized into separate text ranges.
The steps to apply multiple fonts in a paragraph using C# are as follows:
- Create a Document object.
- Add a section and a paragraph to the document.
- Append text to the paragraph using the Paragraph.AppendText() method, which returns a TextRange object.
- Append more text that needs to be styled differently to the paragraph and return different TextRange objects.
- Create a ParagraphStyle object with the basic font information and apply it to the paragraph.
- Change the font name, style, size and text color of the specified text range using the properties under the specific TextRange object.
- Save the document to a Word file.
Here's an example that demonstrates how to apply different fonts to different parts of a paragraph:
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace ApplyDifferentFontsToText
{
class Program
{
static void Main(string[] args)
{
// Create a Document object
Document doc = new Document();
// Add a section
Section section = doc.AddSection();
// Set the page margins
section.PageSetup.Margins.All = 40f;
// Add a paragraph
Paragraph paragraph = section.AddParagraph();
// Append text to the paragraph
TextRange textRange1 = paragraph.AppendText("Spire.Doc for .NET");
TextRange textRange2 = paragraph.AppendText(" is a powerful library designed for developers " +
"working with document processing in the .NET environment. It enables users to ");
TextRange textRange3 = paragraph.AppendText("create, read, modify, and convert");
TextRange textRange4 = paragraph.AppendText(" various types of document formats, such as ");
TextRange textRange5 = paragraph.AppendText("DOC, DOCX, PDF");
TextRange textRange6 = paragraph.AppendText(" and more, without the need for Microsoft Office installed on the machine.");
// Create a ParagraphStyle object
ParagraphStyle style = new ParagraphStyle(doc);
// Set the style name
style.Name = "myStyle";
// Set the font style, name, size and text color
style.CharacterFormat.Bold = false;
style.CharacterFormat.TextColor = Color.Black;
style.CharacterFormat.FontName = "Times New Roman";
style.CharacterFormat.FontSize = 14;
// Add the style the document
doc.Styles.Add(style);
// Apply the style to the paragraph
paragraph.ApplyStyle(style.Name);
// Change the font style of the specified text ranges
textRange1.CharacterFormat.Bold = true;
textRange1.CharacterFormat.TextColor = Color.Purple;
textRange3.CharacterFormat.Bold = true;
textRange3.CharacterFormat.TextColor = Color.Purple;
textRange5.CharacterFormat.Bold = true;
textRange5.CharacterFormat.TextColor = Color.Purple;
// Set the horizontal alignment
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
// Save the document to a docx file
doc.SaveToFile("ApplyDifferentFonts.docx", FileFormat.Docx2019);
// Dispose resources
doc.Dispose();
}
}
}

Use a Custom Font in a Word Document in C#
Spire.Doc supports custom fonts, allowing you to use fonts that are not installed on the system where the application is running. To use a custom font, you need to embed the font file in your application and then specify it in your code.
The steps to utilize custom fonts in a Word document are as follows:
- Create a Document object.
- Add a section and a paragraph to the document.
- Append text to the paragraph.
- Set the custom fonts for the document using the Document.SetCustomFonts() method.
- Embed fonts in the generated file by setting the Document.EmbedFontsInFile property to true.
- Create a ParagraphStyle object.
- Set the font name to your custom font using the ParagraphStyle.CharacterFormat.FontName property.
- Apply the style to the paragraph using the Paragraph.ApplyStyle() method.
- Save the document to a Word file.
Here's an example that demonstrates how to use a custom font:
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace SetFontForParagraph
{
class Program
{
static void Main(string[] args)
{
// Create a Document object
Document doc = new Document();
// Add a section
Section section = doc.AddSection();
// Set the page margins
section.PageSetup.Margins.All = 40f;
// Add a paragraph
Paragraph paragraph = section.AddParagraph();
// Append text to the paragraph
paragraph.AppendText("Spire.Doc for .NET is a powerful library designed for developers " +
"working with document processing in the .NET environment. It enables users to create, " +
"read, modify, and convert various types of document formats, such as DOC, DOCX, PDF, " +
"and more, without the need for Microsoft Office installed on the machine.");
// Specify the private font path
string fontPath = "C:\\Users\\Administrator\\Desktop\\Super Morning.ttf";
// Create an array of Stream objects to hold the font streams
Stream[] fontStreams = new Stream[1];
fontStreams[0] = new FileStream(fontPath, FileMode.Open, FileAccess.Read);
// Set the custom fonts for the document
doc.SetCustomFonts(fontStreams);
// Embed fonts in the generated file
doc.EmbedFontsInFile = true;
// Create a ParagraphStyle object
ParagraphStyle style = new ParagraphStyle(doc);
// Set the paragraph style name
style.Name = "myStyle";
// Set the font name as the name of the private font
style.CharacterFormat.FontName = "Super Morning";
// Set the font style, size and text color
style.CharacterFormat.Bold = false;
style.CharacterFormat.TextColor = Color.Black;
style.CharacterFormat.FontSize = 14;
// Add the style the document
doc.Styles.Add(style);
// Apply the defined style to the paragraph
paragraph.ApplyStyle(style.Name);
// Set the horizontal alignment
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
// Save the document to a docx file
doc.SaveToFile("UseCustomFont.docx", FileFormat.Docx2019);
// Dispose resources
doc.Dispose();
}
}
}

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.