.NET Word Header-Insert Word Header with Image in C# and Visual Basic

Word header, put on the top of one page, is often used to present additional information of one document. The header contents can be document title, a brief summary, company name, author name, page numbers etc. At the beginning, the header is just presented as text. Gradually, the contents are more and more colorful so that header can be used to decorate a document, not only show information, for example special shapes, images etc. In this post, I will introduce a solution to insert Word image header in C# and Visual Basic.

Note: In this example, I use a .NET Word library, Spire.Doc for .NET so that I have added its dll as reference in my project.

Firstly, get default Header property of HeaderFooter class and assign it as value for the new declared HeaderFooter instance. Invoke header.AddParagraph method to add paragraph in header and then set Format property (HorizontalAlignment) for this paragraph.

Secondly, invoke Paragraph.AppendPicture method to add an image in this header paragraph and assign the result as value for DocPicture instance. Then, set Height, Width and TextWrappingStyle properties of this instance.

Thirdly, it is a little dull to have only one image in header. So add some text for header paragraph by invoke Paragrap.AppendText method. And assign the method result as value for a TextRange instance. Set CharacterFormat properties for this instance, including FontName, FontSize and TextColor.

Follow the code blow to realize the function to insert Word image header.

C#

using System.Drawing;

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

 

namespace ImageHeader

{

    class Program

    {

        static void Main(string[] args)

        {

            //Load Document

            Document doc = new Document();

            doc.LoadFromFile(@”E:\Work\Documents\WordDocuments\.NET Framework.docx”);

 

            //Header Paragraph

            HeaderFooter header = doc.Sections[0].HeadersFooters.Header;

            Paragraph para = header.AddParagraph();

            para.Format.HorizontalAlignment = HorizontalAlignment.Right;

 

            //Header Image

            DocPicture pic = para.AppendPicture(Image.FromFile(@”E:\Work\Documents\SampleImage\Microsoft.jpg”));

            pic.Height = 22;

            pic.Width = 30;

            pic.TextWrappingStyle = TextWrappingStyle.Inline;

 

            //Header Text

            TextRange tr=para.AppendText(“Microsoft Technology”);

            tr.CharacterFormat.FontName = “Impact”;

            tr.CharacterFormat.FontSize = 12;

            tr.CharacterFormat.TextColor = Color.DarkBlue;

 

            //Save and Launch

            doc.SaveToFile(“ImageHeader.docx”, FileFormat.Docx);

            System.Diagnostics.Process.Start(“ImageHeader.docx”);

        }

    }

}

 

Visual Basic

Imports System.Drawing

Imports Spire.Doc

Imports Spire.Doc.Documents

Imports Spire.Doc.Fields

 

Namespace ImageHeader

    Friend Class Program

        Shared Sub Main(ByVal args() As String)

            ‘Load Document

            Dim doc As New Document()

            doc.LoadFromFile(“E:\Work\Documents\WordDocuments\.NET Framework.docx”)

 

            ‘Header Paragraph

            Dim header As HeaderFooter = doc.Sections(0).HeadersFooters.Header

            Dim para As Paragraph = header.AddParagraph()

            para.Format.HorizontalAlignment = HorizontalAlignment.Right

 

            ‘Header Image

            Dim pic As DocPicture = para.AppendPicture(Image.FromFile(“E:\Work\Documents\SampleImage\Microsoft.jpg”))

            pic.Height = 22

            pic.Width = 30

            pic.TextWrappingStyle = TextWrappingStyle.Inline

 

            ‘Header Text

            Dim tr As TextRange = para.AppendText(“Microsoft Technology”)

            tr.CharacterFormat.FontName = “Impact”

            tr.CharacterFormat.FontSize = 12

            tr.CharacterFormat.TextColor = Color.DarkBlue

 

            ‘Save and Launch

            doc.SaveToFile(“ImageHeader.docx”, FileFormat.Docx)

            System.Diagnostics.Process.Start(“ImageHeader.docx”)

        End Sub

    End Class

End Namespace

Debug this program and then I get the following result:

Download Spire.Doc for .NET Here

The .NET Office component, Spire.Office for .NET can be used to realize this function as well.

Related Post: Insert Header in Word Document – C#/VB.NET

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s