Canalblog
Editer l'article Suivre ce blog Administration + Créer mon blog
Publicité
SpireXLS
11 juin 2012

With Silverlight-How to Add Header in Word

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.

WordHeader.xaml

<UserControlx:Class="DrawHeader.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="349"d:DesignWidth="474"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

 

    <Gridx:Name="LayoutRoot"Height="350"Width="469">

        <Grid.Background>

            <ImageBrushImageSource="/DrawHeader;component/Images/Technology%20City.jpg" />

        </Grid.Background>

        <sdk:LabelHeight="73"HorizontalAlignment="Center"Margin="20,84,21,193"Name="label1"VerticalAlignment="Center"Width="428"Content="Draw Header in Word"FontSize="36"FontFamily="Arial Black"Foreground="#FF00E100"></sdk:Label>

        <ButtonContent="RUN"Height="36"HorizontalAlignment="Left"Margin="385,263,0,0"Name="button1"VerticalAlignment="Top"Width="58"FontWeight="Bold"Background="#FFF8FCFF"Click="button1_Click" />

    </Grid>

</UserControl>

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

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité