How to Insert Image in Word with Silverlight

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 .NET Word 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.

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

DOWNLOAD Spire.Doc for Silverlight Here

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