How to Set Page in Word Document with Silverlight

Before printing one Word document, users need to set page for having a better layout. The process to set page is called page setup in Word. Page setup includes margins, page orientation, size etc. Generally speaking, users pay more attention on margin settings because contents may not printed completely if margins are not well set.

In this post, I will share my method about how to set Word page with Silverlight. I have prepared a Word document. Then, set its margins (top, bottom, right and left), page size and page orientation. The component, Spire.Doc for Silverlight is used in this example, so I have added its dll file in project at the beginning.

STEPS

Step 1. Design UserControl

Double click MainPage.xaml to design UserControl. Firstly, set background color for UserControl. Secondly, add a label. Change its content and then set label background and content text format. Thirdly, add a button to RUN and set format.

Step 2. Declare SaveFileDialog

Declare a saveFileDialog. Then, set filter for this saveFileDialog, which is the Word format (.doc or .docx) provided with others to choose. In this example, I set the filter format as .docx only.

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

Step 3. Load Document

Right click project to add existed item which is the document which I want to add comment. After adding, click this document to change its Build Action as Embedded Resource.

Double Click RUN button to write code. Declare a new document at the beginning. Next, use foreach sentence to get document name from assembly. If the name is the same with name of embedded resource, load the document.

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

Step 4. Page Setup

Get the first section in Word document. Then, assign value for section.PageSetup.PageSize/Orientation/Margins. Page size is set as A4 and Orientation is set as LandScapge.

            Section section = document.Sections[0];
            section.PageSetup.PageSize = PageSize.A4;
            section.PageSetup.Orientation = PageOrientation.Landscape;
            section.PageSetup.Margins.Top = 55f;
            section.PageSetup.Margins.Bottom = 55f;
            section.PageSetup.Margins.Left = 70f;
            section.PageSetup.Margins.Right = 70f;

Step 5. Save Document

Judge if the saveFileDialog which I declare at step 1 can pop up. If yes, save the encrypted document through it.

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

Full WordTable.xaml.cs

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

namespace SilverlightPageSetup
{
    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(“New Zealand.docx”))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }
            }

            Section section = document.Sections[0];
            section.PageSetup.PageSize = PageSize.A4;
            section.PageSetup.Orientation = PageOrientation.Landscape;
            section.PageSetup.Margins.Top = 55f;
            section.PageSetup.Margins.Bottom = 55f;
            section.PageSetup.Margins.Left = 70f;
            section.PageSetup.Margins.Right = 70f;

            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

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