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.
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.DrawingImports System.Drawing.ImagingImports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.Fields
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)
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
- How to Insert Word Image at Specific Location with C#, VB.NET
- How to point cmake at specific directory for library?
- How to Insert OLE Object (Adobe Acrobat Document) in Word with C#(如何使用C#在Word中插入OLE对象-PDF文件)
- How To: Use DPAPI to Encrypt and Decrypt Data (C#/VB.NET)
- 【GBS】How to communicate with technical professionals at basic level
- Contradiction Detection with Contradiction-Specific Word Embedding
- How to rotate an image with CSS under different browser
- how to post URL with Image on facebook by ShareKit
- How to implement scrolling image with javascript in web page
- How to mount an ISO Image with AIX 6.1 TL4
- QT11 How to set image with QLabel in Qt
- How to insert a resource image, as embedded image in email?
- Peer-to-Peer with VB .NET
- How to register .NET components with COM
- ASP.NET 2.0: How to get a specific ConnectionString from the Web.Config by name
- How to Make Browsers Delete Specific Cookies
- dnf - How to install specific Kernel version
- how to use linux epoll with c
- 藏经阁第2卷-SQL常用的函数
- Red Hat Enterprise 6.3手动安装Thunderbird
- android示例之发送信息
- 大神之路-Android优质资源和项目大整合
- c#生成随机验证码
- How to Insert Word Image at Specific Location with C#, VB.NET
- Qt4.8.3移植总结(转)
- java自定义注解
- android:theme
- spring security 11种过滤器介绍
- 解决Mysql隔夜失效的问题—修改相关timeout默认值(8小时)
- 算法导论 习题15.4-5 15.4-6 找出一个n个数的序列中最长的单调递增子序列
- Android Binder分析五:Java service的获取和调用
- A20 freeAllBuffersLocked called but mQueue is not empty 问题