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

With Silverlight-How to Set Word Page in Word

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.

MainPage.xaml

<UserControlx:Class="SilverlightPageSetup.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="259"d:DesignWidth="401"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"Background="WhiteSmoke">

 

    <Gridx:Name="LayoutRoot"Background="#FF1482DE"Height="257"Width="401">

        <sdk:LabelHeight="111"HorizontalAlignment="Center"Margin="-1,59,1,87"Name="label1"VerticalAlignment="Center"Width="401"Content="Set Word Page with Silverlight"FontFamily="Times New Roman"FontSize="30"Background="WhiteSmoke" />

        <ButtonContent="RUN"Height="37"HorizontalAlignment="Left"Margin="342,221,0,0"Name="button1"VerticalAlignment="Top"Width="59"FontFamily="Arial"FontSize="12"FontWeight="Bold"Background="White"Click="button1_Click" />

    </Grid>

</UserControl>

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

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité