C#/VB.NET: Insert Images in Word

2022-04-01 10:00:00 Written by  Administrator
Rate this item
(0 votes)

Images in Word documents are often closely related to the textual content. Compared with documents full of text, documents with images are more illustrative and attractive. In this article, you will learn how to programmatically insert images in a Word document using Spire.Doc for .NET. With this professional Word library, you can also set the image size, position as well as wrapping styles.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Insert Images and Set their Wrapping Styles in a Word Document

Spire.Doc for .NET supports common wrapping styles such as In Line with Text, Square, Tight, Through, Top and Bottom, Behind the Text as well as In Front of Text. Below are the detailed steps to insert images and then set their wrapping styles.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Get the first section of the Word Document using Document.Sections[] property.
  • Get a specified paragraph of the section using Section.Paragraphs[] property.
  • Load an image and insert the image in the specified paragraph using Paragraph.AppendPicture() method.
  • Set the wrapping style of the image using DocPicture.TextWrappingType property.
  • Save the document to another file using Document.SaveToFile() method.
  • C#
  • VB.NET
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace WordImage
{
    class ImageinWord
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load a sample Word document
            document.LoadFromFile("input.docx");

            //Get the first section 
            Section section = document.Sections[0];

            //Get two specified paragraphs
            Paragraph para1 = section.Paragraphs[5];
            Paragraph para2 = section.Paragraphs[9];

            //Insert images in the specified paragraphs
            DocPicture Pic1 = para1.AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\pic1.jpg"));
            DocPicture Pic2 = para2.AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\pic2.png"));

            //Set wrapping styles to Square and Inline respectively
            Pic1.TextWrappingStyle = TextWrappingStyle.Square;
            Pic2.TextWrappingStyle = TextWrappingStyle.Inline;
 
            //Save the document to file
            document.SaveToFile("InsertImage.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Insert Images in Word

Insert an Image at a Specified Location in a Word document

The DocPicture.HorizontalPosition and DocPicture.VerticalPosition properties offered by Spire.Doc for .NET allows you to insert an image at a specified location. The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Get the first section of the Word Document using Document.Sections[] property.
  • Get a specified paragraph of the section using Section.Paragraphs[] property.
  • Load an image and insert the image to the document using Paragraph.AppendPicture() method.
  • Set the horizontal and vertical position of the image using DocPicture.HorizontalPosition and DocPicture.VerticalPosition properties.
  • Set the height and width of the image using DocPicture.Width and DocPicture.Height properties.
  • Set the wrapping style of the image using DocPicture.TextWrappingType property.
  • Save the document to another file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load a sample Word document
            document.LoadFromFile("input.docx");

            //Get the first section 
            Section section = document.Sections[0];

            //Load an image and insert it to the document
            DocPicture picture = section.Paragraphs[0].AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\pic.jpg"));

            //Set the position of the image 
            picture.HorizontalPosition = 90.0F;
            picture.VerticalPosition = 50.0F;

            //Set the size of the image
            picture.Width = 150;
            picture.Height = 150;

            //Set the wrapping style to Behind
            picture.TextWrappingStyle = TextWrappingStyle.Behind;

            // Save the document to file
            document.SaveToFile("Insert.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Insert Images in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 27 July 2022 02:58