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.
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;
Imports System.Drawing Imports System.Drawing.Imaging Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields
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)
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;
'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
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
![](https://i-blog.csdnimg.cn/blog_migrate/51dcd829b993449ee3b404eaaae34ada.jpeg)