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

With Silverlight - How to Insert Image in Word

Beside text, MS Word provides users with function to insert other contents, for example, shapes, image etc. In this post, I will talk something about image in Word.

Although image is often used to decorate document to make it more appealed, the image is related to contents. For example, if the topic of one document is about animals, author may insert some animal images in document.

Following, I will show a method to insert image in Word with Silverlight. In this example, I use a component, Spire.Doc for Silverlight to make the process be easier and realize this function more quickly. If you want to use the following code, please download and install this component and then add its dll file as reference in your project.

STEPS:

Step 1. Design User Control

Rename MainPage.xaml as InsertImageinWord.xaml. Double click to get UserControl. Firstly, drag UserControl to make it be the most appropriate size and add a background image for it. Then, add a label to show what I will do in this example and change text font style and color. Next, add a button to run.

InsertImageinWord.xaml

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

 

    <Gridx:Name="LayoutRoot"Height="243"Width="426">

        <sdk:LabelHeight="121"HorizontalAlignment="Center"Margin="24,31,12,91"Name="label1"VerticalAlignment="Center"Width="390"Content="Insert Image in Word with Silverlight"FontSize="24"FontFamily="Times New Roman"FontWeight="Bold"Foreground="#FF0AF8D2">sdk:Label>

        <ButtonContent="RUN"Height="36"HorizontalAlignment="Left"Margin="193,174,0,0"Name="button1"VerticalAlignment="Top"Width="65"Click="button1_Click" />

        <Grid.Background>

            <ImageBrushImageSource="/WordImage;component/Images/1242397_105138086_2.jpg" />

        Grid.Background>

    Grid>

UserControl>

Step 2. Declare saveFileDialog

Declare a new saveFileDialog for saving document. Then, set saveFileDialog filter to confirm document format. In this step, I set format extension as .docx.

        private SaveFileDialog saveFileDialog = null;

        public MainPage()

        {

            InitializeComponent();

            this.saveFileDialog = new SaveFileDialog();

            this.saveFileDialog.Filter = "Word Documents(*.docx)|*.docx";

        }

Step 3. Load Image

Before loading image, I should add this image as embedded resource in project. Right click project name and choose Add existed item to add image. After adding, click this image to change its Build Action as Embedded Resource.

Then, double click RUN button to load image. Declare a null stream and then image name from assembly. Judge if the name is the same as image name. If yes, save image to stream.

            Stream stream = null;

            Assembly assembly = this.GetType().Assembly;

            foreach (string name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith("Flower.jpg"))

                {

                    stream = assembly.GetManifestResourceStream(name);

                }

            }

Step 4. Insert Image

Create a new document and add section for this document. Then, add a paragraph and insert image in this paragraph by using paragraph.AppendPicture() method. The parameter passed to this method is stream. Because of image size, set page orientation as landscape for having a more wonderful layout.

            Document document = new Document();

            Section section = document.AddSection();

            Paragraph paragraph= section.AddParagraph();

 

            paragraph.AppendPicture(stream);

            section.PageSetup.Orientation = PageOrientation.Landscape;

Step 5. 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 str = this.saveFileDialog.OpenFile())

                {

                    document.SaveToStream(str, FileFormat.Docx);

                }

            }

Full InsertImageinWord.xaml.cs

using System.Windows;

using System.Windows.Controls;

using System.IO;

using System.Reflection;

using Spire.Doc;

using Spire.Doc.Documents;

namespace WordImage

{

    public partial class MainPage : UserControl

    {

        private SaveFileDialog saveFileDialog = null;

        public MainPage()

        {

            InitializeComponent();

            this.saveFileDialog = new SaveFileDialog();

            this.saveFileDialog.Filter = "Word Documents(*.docx)|*.docx";

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            Stream stream = null;

            Assembly assembly = this.GetType().Assembly;

            foreach (string name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith("Flower.jpg"))

                {

                    stream = assembly.GetManifestResourceStream(name);

                }

            }

 

            Document document = new Document();

            Section section = document.AddSection();

            Paragraph paragraph= section.AddParagraph();

 

            paragraph.AppendPicture(stream);

            section.PageSetup.Orientation = PageOrientation.Landscape;

 

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

            if (result.HasValue && result.Value)

            {

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

                {

                    document.SaveToStream(str, 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é