-
C# 替换Word文本—— 用文档、图片、表格替换文本
编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改。在C# 在word中查找及替换文本一文中,主要介绍了在Word中以文本替换文本的方法,在本篇文章中,将介绍如何用一篇Word文档、图片或者表格来替换文档中的指定文本字符串。示例要点如下:
1. 用文档替换Word中的文本
2. 用图片替换Word中的文本
3. 用表格替换Word中的文本
工具
- Free Spire.Doc for .NET https://www.e-iceblue.cn/Introduce/Free-Spire-Doc-NET.html
下载安装后,注意在程序中添加引用Spire.Doc.dll(如下图),dll文件可在安装路径下的Bin文件夹中获取。
C#代码示例
【示例1】用文档替换Word中的文本
测试文档:
步骤1:加载文档
//加载源文档 Document document = new Document("Original.docx"); //加载用于替换的文档 IDocument replaceDocument = new Document("test.docx");
步骤2:用文档替换文本
document.Replace("History", replaceDocument, false, true);
步骤3:保存文档
document.SaveToFile("result.docx", FileFormat.Docx2013);
替换结果:
全部代码:

using Spire.Doc; using Spire.Doc.Interface; namespace ReplaceTextWithDocument_Doc { class Program { static void Main(string[] args) { //加载源文档 Document document = new Document("Original.docx"); //加载用于替换的文档 IDocument replaceDocument = new Document("test.docx"); //用文档替换源文档中的指定文本 document.Replace("History", replaceDocument, false, true); //保存文档 document.SaveToFile("result.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("result.docx"); } } }
【示例2】用图片替换Word中的文本
测试文档:
步骤1:加载文件
//实例化Document类的对象,并加载测试文档 Document doc = new Document(); doc.LoadFromFile("testfile.docx"); //加载替换的图片 Image image = Image.FromFile("g.png");
步骤2:查找需要替换掉的文本字符串
//获取第一个section Section sec= doc.Sections[0]; //查找文档中的指定文本内容 TextSelection[] selections = doc.FindAllString("Google", true, true); int index = 0; TextRange range = null;
步骤3:用图片替换文本
//遍历文档,移除文本内容,插入图片 foreach (TextSelection selection in selections) { DocPicture pic = new DocPicture(doc); pic.LoadImage(image); range = selection.GetAsOneRange(); index = range.OwnerParagraph.ChildObjects.IndexOf(range); range.OwnerParagraph.ChildObjects.Insert(index, pic); range.OwnerParagraph.ChildObjects.Remove(range); }
步骤4:保存文档
doc.SaveToFile("result.docx", FileFormat.Docx);
替换结果:
全部代码:

using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace ReplaceTextWithImg_Doc { class Program { static void Main(string[] args) { //实例化Document类的对象,并加载测试文档 Document doc = new Document(); doc.LoadFromFile("testfile.docx"); //加载替换的图片 Image image = Image.FromFile("g.png"); //获取第一个section Section sec= doc.Sections[0]; //查找文档中的指定文本内容 TextSelection[] selections = doc.FindAllString("Google", true, true); int index = 0; TextRange range = null; //遍历文档,移除文本内容,插入图片 foreach (TextSelection selection in selections) { DocPicture pic = new DocPicture(doc); pic.LoadImage(image); range = selection.GetAsOneRange(); index = range.OwnerParagraph.ChildObjects.IndexOf(range); range.OwnerParagraph.ChildObjects.Insert(index, pic); range.OwnerParagraph.ChildObjects.Remove(range); } //保存文档 doc.SaveToFile("result.docx", FileFormat.Docx); System.Diagnostics.Process.Start("result.docx"); } } }
【示例3】用表格替换Word中的文本
测试文档:
步骤1:加载文档
Document doc = new Document(); doc.LoadFromFile("test.docx");
步骤2:查找关键字符串
Section section = doc.Sections[0]; TextSelection selection = doc.FindString("参考附录", true, true);
步骤3:获取关键字符串所在段落的索引
TextRange range = selection.GetAsOneRange(); Paragraph paragraph = range.OwnerParagraph; Body body = paragraph.OwnerTextBody; int index = body.ChildObjects.IndexOf(paragraph);
步骤4:添加表格
Table table = section.AddTable(true); table.ResetCells(2, 3); range = table[0, 0].AddParagraph().AppendText("管号(McFarland)"); range = table[0, 1].AddParagraph().AppendText("0.5"); range = table[0, 2].AddParagraph().AppendText("1"); range = table[1, 0].AddParagraph().AppendText("0.25%BaCl2(ml)"); range = table[1, 1].AddParagraph().AppendText("0.2"); range = table[1, 2].AddParagraph().AppendText("0.4");
步骤5:移除段落,插入表格
body.ChildObjects.Remove(paragraph);
body.ChildObjects.Insert(index, table);
步骤6:保存文档
doc.SaveToFile("result.doc", FileFormat.Doc);
替换结果:
全部代码:

using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace ReplaceTextWithTable_Doc { class Program { static void Main(string[] args) { //实例化Document类的对象,并加载测试文档 Document doc = new Document(); doc.LoadFromFile("test.docx"); //查找关键字符串文本 Section section = doc.Sections[0]; TextSelection selection = doc.FindString("参考附录", true, true); //获取关键字符串所在的段落 TextRange range = selection.GetAsOneRange(); Paragraph paragraph = range.OwnerParagraph; Body body = paragraph.OwnerTextBody; int index = body.ChildObjects.IndexOf(paragraph); //添加一个两行三列的表格 Table table = section.AddTable(true); table.ResetCells(2, 3); range = table[0, 0].AddParagraph().AppendText("管号(McFarland)"); range = table[0, 1].AddParagraph().AppendText("0.5"); range = table[0, 2].AddParagraph().AppendText("1"); range = table[1, 0].AddParagraph().AppendText("0.25%BaCl2(ml)"); range = table[1, 1].AddParagraph().AppendText("0.2"); range = table[1, 2].AddParagraph().AppendText("0.4"); //移除段落,插入表格 body.ChildObjects.Remove(paragraph); body.ChildObjects.Insert(index, table); //保存文档 doc.SaveToFile("result.doc", FileFormat.Doc); System.Diagnostics.Process.Start("result.doc"); } } }
以上是本次关于“C# 用文档、图片、表格替换Word中的文本字符串的”的全部内容。
(本文完)
原文链接:https://www.cnblogs.com/Yesi/p/10031817.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数