How to Add Word Page Borders with Silverlight

In order to have a much better layout, users may pay attention on Word page setting, such as page setup and page background. In this post, I want to talk something about page borders, which is one kind of settings in page background.

Page borders are applied for whole page. All the contents will be put in a box which is formed with borders. Because of borders, contents can be presented more obviously. Also, users can set different types and colors for page border to make the appearance more wonderful.

Then, I will introduce a method about how to add Word page borders with Silverlight. In this example, I use the Silverlight Word component, Spire.Doc for Silverlight, so I have added its dll file in my project.

Document without Borders

 

STEPS

Step 1. Declare SaveFileDialog

Declare a SaveFileDialog for save document. Then, set filter for this SaveFileDialog and I set the filter document format as .docx.

Step 2. Load Document

Right click project name to add existing item (Word document). After adding, change its Build Action as Embedded Resource.

Add a run button in UserControl. Double click the button and write code. Declare document and assembly. Use foreach sentence to get name string from assembly. If the name is the same as embedded resource name, load this embedded resource.

Step 3. Add Word Page Borders

Get the section of document and then add borders for this section. Firstly, set the border type and color. Secondly, for confirming contents are all put in box formed with borders, set right and left space between contents and borders.

Step 4. Save Document

Judge if the SaveFileDialog which is declared in first step can pop up. If the result is true, use document.SaveToStrem() method to save this document via SaveFileDialog.

Full BorderofPage.xaml.cs

using System;

using System.Windows;

using System.Windows.Controls;

using System.Drawing;

using System.IO;

using System.Reflection;

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace PAGEBORDER

{

    public partial class MainPage : UserControl

    {

        //Declare SaveFileDialog

        private SaveFileDialog saveFileDialog = new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

            this.saveFileDialog.Filter = “Word Document (*.docx)|*.docx”;

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            //Load Document

            Document document = new Document();

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith(“Humor Them.docx”))

                {

                    using (Stream docStream = assembly.GetManifestResourceStream(name))

                    {

                        document = new Document(docStream, FileFormat.Docx);

                    }

                }

            }

 

            //Add Border

            Section section = document.Sections[0];

            section.PageSetup.Borders.BorderType = BorderStyle.Engrave3D;

            section.PageSetup.Borders.Color = Color.AliceBlue;

            section.PageSetup.Borders.Left.Space = 50;

            section.PageSetup.Borders.Right.Space = 50;

 

            //Save Document

            bool? result = this.saveFileDialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream stream = this.saveFileDialog.OpenFile())

                {

                    document.SaveToStream(stream, FileFormat.Docx);

                }

            }

        }

    }

}

 

Result

 

Freely Download Spire.Doc for Silverlight

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.

1 thought on “How to Add Word Page Borders with Silverlight”

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