Insert Image at Specified Location in C#, VB.NET

Image can add interest and take effect to your Word documents. Suppose you've written an article introducing a city about its beautiful scenery. Just using words describe, you cannot present perfect scenery to your readers, because that page of text looks indistinct and dull. You need image to embellish your scenery.

Spire.Doc for .NET, a professional .NET word component to fast generate, open, modify and save Word documents without using MS Office Automation, enables users to insert image into Word document and set its size according to page by using C#.NET. This guide introduces an easy method how to insert image via Spire.Doc for .NET.

At first, create new Word document and add section, paragraph for this document. Then, insert image in the new created paragraph. You can set this image's size, position, rotate it and choice the wrapping-style about the text. Download and Install Spire.Doc for .NET. Use the following code to insert image in Word by using C#.

The sample demo shows how to insert an image into a document.

Insert Image in Word Document

using System;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace Insert_image_to_Word_Document
{
    class Program
    {
        static void Main(string[] args)
        {
 //Open a blank word document as template
            Document document = new Document(@"Blank.doc");
            Section section = document.Sections[0];
            Paragraph paragraph
                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
            paragraph.AppendText("The sample demonstrates how to insert an image into a document.");
            paragraph.ApplyStyle(BuiltinStyle.Heading2);
            paragraph = section.AddParagraph();
  
            //get original image 
            Bitmap p = new Bitmap(Image.FromFile(@"Word.jpg")); 
           
            //rotate image and insert image to word document
            p.RotateFlip(RotateFlipType.Rotate90FlipX);

            DocPicture picture = document.Sections[0].Paragraphs[0].AppendPicture(p);
   
            //set image's position
            picture.HorizontalPosition = 50.0F;
            picture.VerticalPosition = 60.0F;

            //set image's size
            picture.Width = 200;
            picture.Height = 200;

            //set textWrappingStyle with image;
            picture.TextWrappingStyle = TextWrappingStyle.Through;

            //Save doc file.
            document.SaveToFile("Sample.doc", FileFormat.Doc);

            //Launching the MS Word file.            System.Diagnostics.Process.Start("Sample.doc");
        }
       }
}

If you have finished the tutorial Spire.Doc Quick Start, the steps above will be simpler.

With Spire.Doc, you can insert an image into your Word documents and do more same thing in your ASP.NET, WPF and Silverlight applications without Word automation and any other third party add-ins.

Additional Info

  • tutorial_title: Insert Image at Specified Location
Last modified on Friday, 03 September 2021 03:45