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

With Silverlight-How to Add Footer for Word Document

Footer, often added on the bottom of one Word page, shows additional information about document. The information can be document title, company name, author name, page number etc. Besides text, footer can be image as well, such as company logo, stamp and so on.

In this post, I will share my method about how to add footer in Word document with Silverlight. I will create a new Word document and then add footer for it. The footer is text and will be formatted.

STEPS:

Step 1. Design User Control

Rename MainPage.xaml as Footer.xaml. Double click Footer.xaml to design UserControl. At the beginning, judge size. Add a label and change its contents as “Draw Footer in Word”. Then, set text font style and color. Next, add a button to run. Finally, set UserControl background as image.

Footer.xaml

<UserControlx:Class="WordFooter.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="265"d:DesignWidth="428"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

 

    <Gridx:Name="LayoutRoot"Height="265"Width="428">

        <ButtonContent="RUN"Height="36"HorizontalAlignment="Left"Margin="189,188,0,0"Name="button1"VerticalAlignment="Top"Width="72"FontWeight="Bold"Background="#FF1CF347"Click="button1_Click">

            <Button.BorderBrush>

                <LinearGradientBrush>

                    <GradientStopColor="#FFA3AEB9"Offset="0" />

                    <GradientStopColor="#FF8399A9"Offset="0.375" />

                    <GradientStopColor="#FF718597"Offset="0.375" />

                    <GradientStopColor="#FF087BD0"Offset="1" />

                </LinearGradientBrush>

            </Button.BorderBrush>

        </Button>

        <sdk:LabelHeight="130"HorizontalAlignment="Center"Margin="24,33,28,102"Name="label1"VerticalAlignment="Center"Width="375"Content="Draw Footer in Word"FontSize="30"FontFamily="Arial Black">

            <sdk:Label.Foreground>

                <LinearGradientBrushEndPoint="1,0.5"StartPoint="0,0.5">

                    <GradientStopColor="Black"Offset="0" />

                    <GradientStopColor="#FFE6FFFF"Offset="0.008" />

                </LinearGradientBrush>

            </sdk:Label.Foreground>

        </sdk:Label>

        <Grid.Background>

            <ImageBrushImageSource="/WordFooter;component/Images/BG.jpg" />

        </Grid.Background>

    </Grid>

</UserControl>

Step 2. Declare saveFileDialog

Declare a saveFileDialog for save Word document. Then, set filter for this saveFileDialog. In this example, I set filter format as .docx.

        private SaveFileDialog saveFiledialog = new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

            this.saveFiledialog.Filter = "Word Document (*.docx)|*.docx";

        }

Step 3. Add Footer

Declare a new word document. Then, add a section in this document. Next, add footer in this section.

            Document document = new Document();

            Section section = document.AddSection();

            HeaderFooter footer = section.HeadersFooters.Footer;

Create a footer paragraph, append text for this paragraph. The text is footer content. Then, set format for footer content, including font style, color and alignment.

            Paragraph footerParagraph = footer.AddParagraph();

            TextRange text = footerParagraph.AppendText("Word Footer");

 

            text.CharacterFormat.FontName = "Calibri";

            text.CharacterFormat.FontSize = 14;

            text.CharacterFormat.TextColor = Color.DarkOrange;

            text.CharacterFormat.Bold = true;

            text.CharacterFormat.Italic = true;

            footerParagraph.Format.HorizontalAlignment

                = Spire.Doc.Documents.HorizontalAlignment.Right;

In order to separate footer from document contents, add top border for footer. Then, set border style and color.

            footerParagraph.Format.Borders.Top.BorderType

                = Spire.Doc.Documents.BorderStyle.ThinThinSmallGap;

            footerParagraph.Format.Borders.Top.Space = 0.15f;

            footerParagraph.Format.Borders.Color = Color.CadetBlue;

Step 4. Save File

Judge if the saveFileDialog which is declared in step 1 can pop up. If so, save document which image is inserted in 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

usingSystem.Windows;

usingSystem.Windows.Controls;

usingSystem.IO;

usingSystem.Drawing;

 

usingSpire.Doc;

usingSpire.Doc.Documents;

usingSpire.Doc.Fields;

 

 

namespaceWordFooter

{

    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();

            Section section = document.AddSection();

            HeaderFooter footer = section.HeadersFooters.Footer;

 

            Paragraph footerParagraph = footer.AddParagraph();

            TextRange text = footerParagraph.AppendText("Word Footer");

 

            text.CharacterFormat.FontName = "Calibri";

            text.CharacterFormat.FontSize = 14;

            text.CharacterFormat.TextColor = Color.DarkOrange;

            text.CharacterFormat.Bold = true;

            text.CharacterFormat.Italic = true;

            footerParagraph.Format.HorizontalAlignment

                = Spire.Doc.Documents.HorizontalAlignment.Right;

 

            footerParagraph.Format.Borders.Top.BorderType

                = Spire.Doc.Documents.BorderStyle.ThinThinSmallGap;

            footerParagraph.Format.Borders.Top.Space = 0.15f;

            footerParagraph.Format.Borders.Color = Color.CadetBlue;

 

            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é