Easy Method to Insert Image in Word with C#/VB.NET

We often find that there will be some images inserted in document. These images are related to contents and can make readers to learn contents more clearly. For example, I will insert an image to explain how to use a formula. Also, sometimes, people insert image in Word for beautifying document appearance, such as inserting poster when introducing a film.

In this post, I want to show a method about how to insert image in Word with C#/VB.NET

Note: I use Spire.Doc for .NET to realize this function more conveniently. If you want to use the following code, please add Spire.Doc dll file as reference.

Detailed Step Shown as Following:

  1. Create a new Word document.
  2. Add section and paragraph.
  3. Define a style for paragraph, including font style, color and line spacing.
  4. Append text in paragraph. The contents show what I want to do in this example.
  5. Add a new paragraph. Load image from system and use paragraph.AppendPicture(image) method to insert this image in paragraph.
  6. Save and launch file.

Main Coding:

C#

using System;

using System.Drawing;

using Spire.Doc;

using Spire.Doc.Documents;

namespace InsertImage

{

    class Images

    {

        static void Main(string[] args)

        {

            //Create Document

            Document document = new Document();

            //Add Section and Paragraph

            Section section = document.AddSection();

            Paragraph paragraph = section.AddParagraph();

            //Style

            ParagraphStyle style = new ParagraphStyle(document);

            style.Name = “Style”;

            style.CharacterFormat.Font = new Font(“Century Gothic”, 14f);

            style.CharacterFormat.TextColor = Color.DarkBlue;

            style.ParagraphFormat.LineSpacing = 1.5F * 12F;

            style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;

            document.Styles.Add(style);

            //Add Text

            paragraph.AppendText(“This Example Presents Method to Insert Image in MS Word.”);

            paragraph.ApplyStyle(style.Name);

            //Insert Image

            paragraph = section.AddParagraph();

            Image image = Image.FromFile(@”D:\work\My Documents\Flower in Cup.jpg”);

            paragraph.AppendPicture(image);

            //Save and Launch

            document.SaveToFile(“Image.docx”, FileFormat.Docx);

            System.Diagnostics.Process.Start(“Image.docx”);

        }

    }

}

VB.NET

Imports System

Imports System.Drawing

Imports Spire.Doc

Imports Spire.Doc.Documents

Namespace InsertImage

    Friend Class Images

        Shared Sub Main(ByVal args() As String)

            ‘Create Document

            Dim document As New Document()

            ‘Add Section and Paragraph

            Dim section As Section = document.AddSection()

            Dim paragraph As Paragraph = section.AddParagraph()

            ‘Style

            Dim style As New ParagraphStyle(document)

            style.Name = “Style”

            style.CharacterFormat.Font = New Font(“Century Gothic”, 14.0F)

            style.CharacterFormat.TextColor = Color.DarkBlue

            style.ParagraphFormat.LineSpacing = 1.5F * 12.0F

            style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple

            document.Styles.Add(style)

            ‘Add Text

            paragraph.AppendText(“This Example Presents Method to Insert Image in MS Word.”)

            paragraph.ApplyStyle(style.Name)

            ‘Insert Image

            paragraph = section.AddParagraph()

            Dim image As Image = Image.FromFile(“D:\work\My Documents\Flower in Cup.jpg”)

            paragraph.AppendPicture(image)

            ‘Save and Launch

            document.SaveToFile(“Image.docx”, FileFormat.Docx)

            System.Diagnostics.Process.Start(“Image.docx”)

        End Sub

    End Class

End Namespace

Result Shown as Following:

Freely Download Spire.Doc for .NET

Advertisements

3 thoughts on “Easy Method to Insert Image in Word with C#/VB.NET

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