Blog author:
Lacy | Posted on: 5/30/2012 | Category:
C# Blogs | Views: 6353 | Status:
[Member] |
Points: 75
|
Alert Moderator
Header and footer in Word show the additional information of this document. They are maybe not the main role but most times they are indispensible for the whole document. For example, the specialized books in a company always are printed a company logo on it to clearly tell users what the books come from. Using Spire.Doc, you can easily change word header & footer and header image with C#, VB.NET as well as add them.
How to change word header & footer and header image information with C#,VB.NET
Spire.Doc not only can change header & footer text 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 2 or above.
2. Add System.Drawing and Spire.Doc dll as reference in Project.
3. Add the following using at the top of the method.
C#
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Collections;
VB.NETImports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Doc.Collections
Step2. Load a word document from systemC# 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ë"
Step4. Save and launch the fileC# 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
Original Header and Footer
Change Header & Footer Text and Image
More About Spire.Doc for .NET
Spire.Doc for .NET 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. It supports C#, VB.NET, ASP.NET, ASP.NET MVC.
Found interesting? Add this to: