Word控件Spire.Doc 【文本】教程(14) ;如何用图片替换Word中的文字

注意:开始之前,请下载最新版本的Spire.Doc,并将Spire.Doc.dll添加到bin文件夹中,作为visual studio的参考。

如何用图片替换Word中的文字

第 1 步:加载示例 Word 文档和用于替换文本的图像。

Document document = new Document();document.LoadFromFile("s.docx");Image image = Image.FromFile("2.bmp");

第 2 步:在文档中找到字符串“E-iceblue”。

TextSelection[] selections = document.FindAllString("E-iceblue", true, true);int index = 0;TextRange range = null;

第 3 步:删除文本并将其替换为图像

foreach (TextSelection selection in selections){DocPicture pic = new DocPicture(document);pic.LoadImage(image);range = selection.GetAsOneRange();index = range.OwnerParagraph.ChildObjects.IndexOf(range);range.OwnerParagraph.ChildObjects.Insert(index, pic);range.OwnerParagraph.ChildObjects.Remove(range);}

第 4 步:保存并启动文档以查看效果。

document.SaveToFile("Sample.doc", FileFormat.Doc);System.Diagnostics.Process.Start("Sample.doc");

效果

如何用图片替换Word中的文字

完整代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using System.Drawing;namespace Replace_Text_with_Image{class Program{static void Main(string[] args){Document document = new Document();document.LoadFromFile("s.docx");Image image = Image.FromFile("2.bmp");TextSelection[] selections = document.FindAllString("E-iceblue", true, true);int index = 0;TextRange range = null;foreach (TextSelection selection in selections){DocPicture pic = new DocPicture(document);pic.LoadImage(image);range = selection.GetAsOneRange();index = range.OwnerParagraph.ChildObjects.IndexOf(range);range.OwnerParagraph.ChildObjects.Insert(index, pic);range.OwnerParagraph.ChildObjects.Remove(range);}document.SaveToFile("Sample.doc", FileFormat.Doc);System.Diagnostics.Process.Start("Sample.doc");}}}

欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年7月12日
下一篇 2022年7月12日

相关推荐

发表回复

登录后才能评论