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

来源:互联网 发布:软件工程设计招聘 编辑:程序博客网 时间:2024/06/05 17:39

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.


Spire.Doc Download Zip

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.DrawingImports System.Drawing.ImagingImports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.Fields

Procedure

Step1. Load a word document from system.

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

VB.NET Code:
'load the word documentDim 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
原创粉丝点击