How to Add Header in Word Document with Silverlight

Header, similar to footer, is used to show some additional information about document. Its contents can be same with footer, for example, document title, page number etc. However, if one document includes both header and footer, the contents will be different. In this post, I will show the method to add header in Word with Silverlight.

At the beginning, I have prepared a Word document. I will add a header for it to present document title and set format for this header. Also, the component, Spire.Doc for Silverlight is used in this example to realize this function more quickly and easily.

STEPS:

Step 1. Design User Control

Rename MainPage.xaml as WordHeader.xaml and double click it to design UserControl. Set background as an image and then add a label to show what I will do. Also, set format for label contents. Finally, add a button to run.

Step 2. Declare saveFileDialog

Declare a saveFileDialog for saving Word document. Also, set saveFileDialog filter to set which kind of format to choose. In this example, I set it as .docx.

        private SaveFileDialog saveFiledialog = new SaveFileDialog();
        public MainPage()
        {
            InitializeComponent();
            this.saveFiledialog.Filter = “Word Document (*.docx)|*.docx”;
        }

Step 3. Load Document

Firstly, right click project name to add existed item (Word document I prepared). After adding, click this document and change its Build Action as Embedded Resource.

Secondly, double click run button to write code. Declare a new document at the beginning. Then, judge if the document name in assembly is the same as embedded resource name. If so, get this document.

            Document document = new Document();
            Assembly assembly = this.GetType().Assembly;
            foreach (String name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith(“welcome.docx”))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }
            }

Step 3. Add Header

Get section in this loaded document and then add header in this section. Next, add a header paragraph to add text which presents header contents.

            Section section = document.Sections[0];
            HeaderFooter header = section.HeadersFooters.Header;
            Paragraph headerParagraph = header.AddParagraph();
            TextRange text = headerParagraph.AppendText(“Welcome to Our Town”);

In order to make the header more wonderful, set format for header text, including font style, color and alignment.

            text.CharacterFormat.FontName = “Arial Narrow”;
            text.CharacterFormat.FontSize = 12;
            text.CharacterFormat.Italic= true;
            text.CharacterFormat.TextColor = Color.DarkSlateGray;
            headerParagraph.Format.HorizontalAlignment
                = Spire.Doc.Documents.HorizontalAlignment.Right;

Also, add a border to separate header and document body and set border format.

            headerParagraph.Format.Borders.Bottom.BorderType
                = Spire.Doc.Documents.BorderStyle.ThickThinMediumGap;
            headerParagraph.Format.Borders.Bottom.Space = 0.05f;
            headerParagraph.Format.Borders.Bottom.Color = Color.AliceBlue;

Step 4. Save File

Judge if the saveFileDialog which I declare in the first step can pop up. If yes, save the Word document with header through it.

            bool? result = this.saveFiledialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFiledialog.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }

Full Footer.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Reflection;
using System.IO;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace DrawHeader
{
    public partial class MainPage : UserControl
    {
        private SaveFileDialog saveFiledialog = new SaveFileDialog();
        public MainPage()
        {
            InitializeComponent();
            this.saveFiledialog.Filter = “Word Document (*.docx)|*.docx”;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document document = new Document();
            Assembly assembly = this.GetType().Assembly;
            foreach (String name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith(“welcome.docx”))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }
            }

            Section section = document.Sections[0];

            HeaderFooter header = section.HeadersFooters.Header;
            Paragraph headerParagraph = header.AddParagraph();

            TextRange text = headerParagraph.AppendText(“Welcome to Our Town”);
            text.CharacterFormat.FontName = “Arial Narrow”;
            text.CharacterFormat.FontSize = 12;
            text.CharacterFormat.Italic= true;
            text.CharacterFormat.TextColor = Color.DarkSlateGray;
            headerParagraph.Format.HorizontalAlignment
                = Spire.Doc.Documents.HorizontalAlignment.Right;

            headerParagraph.Format.Borders.Bottom.BorderType
                = Spire.Doc.Documents.BorderStyle.ThickThinMediumGap;
            headerParagraph.Format.Borders.Bottom.Space = 0.05f;
            headerParagraph.Format.Borders.Bottom.Color = Color.AliceBlue;

            bool? result = this.saveFiledialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFiledialog.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }
        }
    }
}

RESULT

_______________________________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc for Silverlight

Click Here to DOWNLOAD Spire.Doc for Silverlight

Spire.Office also can be used to realize this function

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

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