How to Insert Word Image at Specific Location with C#, VB.NET

http://www.dotnetfunda.com/blogs/lacy/2560/how-to-insert-word-image-at-specific-location-with-csharp-vbnet


Word allows its users to easily insert image in the document, which provides great convenience to people and makes the word document looks colorful. But more often in our daily work, set the image location is more important than just insert the image. Especially for newspaper and magazine editors, they not only need to place the text in the right place, also have to put the image in suitable place since the location of image influences the whole edition work. So it is very necessary to insert the image in specific location.

In this article, I will show you a simple method to insert word image at specific location with C#, VB.NET. I will insert three images in my word document, and each has a certain location of their own. Of course, I need a .NET word component Spire.Doc to help me finish this task through three steps. If you also want to give it a try, please freely install it on your system. It really works very well.



How to insert word image in specific location with C#, VB.NET

Before we start, please do some prepare work. At the very beginning, you need to create a new project in Visual Studio and set your target Framework to be .NET Framework 4. Then, add System. Drawing and Spire. Doc DLL as reference, finally please add the following using at the top of the method:

C#
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

VB.NET 
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Procedure

Step1. Load a word document from system.

C# Code:
//load the word document
Document doc = new Document();
doc.LoadFromFile(@"E:\Jane Eyre.docx", FileFormat.Doc);

VB.NET Code:
'load the word document
Dim doc As New Document()
doc.LoadFromFile("E:\Jane Eyre.docx", FileFormat.Doc)

Step2. Insert images and set locations

C# Code:

           //load the first image
            Image image = Image.FromFile(@"E:\Eyre.jpg");
            //add the first image into word document at the specific paragraph
            DocPicture picture1 = doc.Sections[0].Paragraphs[1].AppendPicture(image);
            //set the first image's alignment and TextWrappingStyle
            picture1.VerticalAlignment = ShapeVerticalAlignment.Top;
            picture1.HorizontalAlignment = ShapeHorizontalAlignment.Left;
            picture1.TextWrappingStyle = TextWrappingStyle.Square;
            //load the second image
            Image image2 = Image.FromFile(@"E:\Jane.jpg");
            //add the second image into doc template at the specific paragraph
            DocPicture picture2 = doc.Sections[0].Paragraphs[2].AppendPicture(image2);
            //set the second image's alignment and TextWrappingStyle
            picture2.VerticalAlignment = ShapeVerticalAlignment.Outside;
            picture2.HorizontalAlignment = ShapeHorizontalAlignment.Left;
            picture2.TextWrappingStyle = TextWrappingStyle.Through;
            //load the third image
            Image image3 = Image.FromFile(@"E:\Jane Eyre.jpg");
            //add the third image into doc template at the specific paragraph
            DocPicture picture3 = doc.Sections[0].Paragraphs[1].AppendPicture(image3);
            //set the third image's alignment and TextWrappingStyle
            picture3.VerticalAlignment = ShapeVerticalAlignment.Center;
            picture3.HorizontalAlignment = ShapeHorizontalAlignment.Outside;
            picture3.TextWrappingStyle = TextWrappingStyle.InFrontOfText;

  VB.NET Code:
                     'load the first image
                       Dim image__1 As Image = Image.FromFile("E:\Eyre.jpg") 
                       'add the first image into word document at the specific paragraph
                       Dim picture1 As DocPicture = doc.Sections(0).Paragraphs(1).AppendPicture(image__1)
                       'set the first image's alignment and TextWrappingStyle
                       picture1.VerticalAlignment = ShapeVerticalAlignment.Top
                       picture1.HorizontalAlignment = ShapeHorizontalAlignment.Left
                       picture1.TextWrappingStyle = TextWrappingStyle.Square
                       'load the second image
                       Dim image2 As Image = Image.FromFile("E:\Jane.jpg")
                       'add the second image into doc template at the specific paragraph
                       Dim picture2 As DocPicture = doc.Sections(0).Paragraphs(2).AppendPicture(image2) 
                       'set the second image's alignment and TextWrappingStyle
                       picture2.VerticalAlignment = ShapeVerticalAlignment.Outside
                       picture2.HorizontalAlignment = ShapeHorizontalAlignment.Left
                       picture2.TextWrappingStyle = TextWrappingStyle.Through
                       'load the third image
                       Dim image3 As Image = Image.FromFile("E:\Jane Eyre.jpg")
                       'add the third image into doc template at the specific paragraph
                       Dim picture3 As DocPicture = doc.Sections(0).Paragraphs(1).AppendPicture(image3)
                       'set the third image's alignment and TextWrappingStyle
                       picture3.VerticalAlignment = ShapeVerticalAlignment.Center
                       picture3.HorizontalAlignment = ShapeHorizontalAlignment.Outside
                       picture3.TextWrappingStyle = TextWrappingStyle.InFrontOfText

Step3.Save and launch the file

C# Code:

         //save the doc
         doc.SaveToFile(@"..\..\result.doc", FileFormat.Doc);
         System.Diagnostics.Process.Start(@"..\..\result.doc");

VB.NET Code:

        'save the doc
        doc.SaveToFile("..\..\result.doc", FileFormat.Doc)
        System.Diagnostics.Process.Start("..\..\result.doc")


Preview  
                

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值