How to change word header & footer and header image with C#,VB.NET
Spire.Doc not only can change header & footer information, but also can change its header image and set image format with C#, VB.NET. The whole task can be realized by the below four steps.
Step1. Create a new project
1. Create a new project in Visual Studio, and Set the project Target framework in Property to be .NET Framework 4.
2. Add System.Drawing and Spire.Doc dll as reference in Project.
3. Add the following using at the top of the method.
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Collections;
VB.NET
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Doc.Collections
C# Code:
Document doc = new Document();
doc.LoadFromFile(@"E:\JaneEyre.docx", FileFormat.Docx);
VB.NET Code:
Dim doc As New Document()
doc.LoadFromFile("E:\JaneEyre.docx", FileFormat.Docx)
Step3. Change word header & footer and header image.
C# Code:
HeaderFooter header = doc.Sections[0].HeadersFooters.Header;
header.Paragraphs[0].Text = "Preview";
Image logo = Image.FromFile(@"D:\E-ICEBLUE.png");
header.Paragraphs[0].AppendPicture(logo).TextWrappingStyle = TextWrappingStyle.Tight;
HeaderFooter footer = doc.Sections[0].HeadersFooters.Footer;
footer.Paragraphs[0].Text = "Author: Charlotte Brontë";
VB.NET Code:
Dim header As HeaderFooter = doc.Sections(0).HeadersFooters.Header
header.Paragraphs(0).Text = "Preview"
Dim logo As Image = Image.FromFile("D:\E-ICEBLUE.png")
header.Paragraphs(0).AppendPicture(logo).TextWrappingStyle = TextWrappingStyle.Tight
Dim footer As HeaderFooter = doc.Sections(0).HeadersFooters.Footer
footer.Paragraphs(0).Text = "Author: Charlotte Brontë"
C# Code:
doc.SaveToFile("Sample.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Sample.docx");
VB.NET Code:
doc.SaveToFile("Sample.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("Sample.docx")
Preview
Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight.
More Word Functions