How to Insert Word Image at Specific Location with C#, VB.NET
2014-04-24 10:40
3723 查看
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#
VB.NET
Procedure
Step1. Load a word document from system.
C# Code:
VB.NET Code:
Step2. Insert images and set locations
C# Code:
VB.NET Code:
Step3.Save and launch the file
C# Code:
VB.NET Code:
Preview
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.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
相关文章推荐
- How to call webservices with ASP.NET at Run time?
- How to Insert OLE Object (Adobe Acrobat Document) in Word with C#(如何使用C#在Word中插入OLE对象-PDF文件)
- How to Create Text Image on the fly with ASP.NET
- how to use Wordnet with Java?
- [转][ASP.NET]How to easily insert row in GridView with SqlDataSource?
- How to use JabRef (BibTeX) with Microsoft Word
- How to Integrate .Net / Flex 3 with FluorineFX — Part I: The Server
- How to specify image folder path for each user when use TinyMce ImageManager with Symfony
- How To Use the PivotTable Office Web Component with VB (转载自微软)
- How to fill the background with image in landscape in IOS? 如何使image水平铺满屏幕
- ASP.NET: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. on Win7 64bit
- How to integrate custom security policy with Windows domain authentication in ASP.NET
- [转]How to insert a row between two rows in an existing excel with HSSF (Apache POI)
- How to create own operator with python in mxnet?
- how to make dword with high/low word or high/low byte
- How to run 32 bit .NET Windows apps on 64 bit Windows (including with IEExec)
- [VB] How to load a controll at runtime.
- How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET(利用Ado.net导出到Excel)
- How To Use Proxy Server To Access Internet at Shell Prompt With http_proxy Variable. [reprint]
- How to connect to MySQL database from Visual Studio VS2010 – problems with NET connectors