C# 替换Word文本—— 用文档、图片、表格替换文本
编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改。在C# 在word中查找及替换文本一文中,主要介绍了在Word中以文本替换文本的方法,在本篇文章中,将介绍如何用一篇Word文档、图片或者表格来替换文档中的指定文本字符串。示例要点如下:
1. 用文档替换Word中的文本
2. 用图片替换Word中的文本
3. 用表格替换Word中的文本
工具
下载安装后,注意在程序中添加引用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[]; //查找文档中的指定文本内容
TextSelection[] selections = doc.FindAllString("Google", true, true);
int index = ;
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[]; //查找文档中的指定文本内容
TextSelection[] selections = doc.FindAllString("Google", true, true);
int index = ;
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[];
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(, );
range = table[, ].AddParagraph().AppendText("管号(McFarland)");
range = table[, ].AddParagraph().AppendText("0.5");
range = table[, ].AddParagraph().AppendText("");
range = table[, ].AddParagraph().AppendText("0.25%BaCl2(ml)");
range = table[, ].AddParagraph().AppendText("0.2");
range = table[, ].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[];
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(, );
range = table[, ].AddParagraph().AppendText("管号(McFarland)");
range = table[, ].AddParagraph().AppendText("0.5");
range = table[, ].AddParagraph().AppendText("");
range = table[, ].AddParagraph().AppendText("0.25%BaCl2(ml)");
range = table[, ].AddParagraph().AppendText("0.2");
range = table[, ].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中的文本字符串的”的全部内容。
(本文完)
C# 替换Word文本—— 用文档、图片、表格替换文本的更多相关文章
- PictureCleaner 官方版 v1.1.3.04061,免费的图片校正及漂白专业工具,专业去除文档图片黑底麻点杂色,规格化A4、B5尺寸输出,还你一个清晰的文本。
当家长多年,每天都要拍照试卷打印.用App去掉图片黑底就成了每天必备工作.可是,有些图片文件不是来自手机,所以需要一个电脑版的图片漂白工具.经过一个多月努力,PictureCleaner官方版诞生了 ...
- 基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理
http://www.cnblogs.com/wuhuacong/p/4175266.html 在一般的管理系统模块里面,越来越多的设计到一些常用文档的上传保存操作,其中如PDF.Word.Excel ...
- Aspose.Words操作word生成PDF文档
Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...
- 用R创建Word和PowerPoint文档--转载
https://www.jianshu.com/p/7df62865c3ed Rapp --简书 Microsoft的Office软件在办公软件领域占有绝对的主导地位,几乎每个职场人士都必须掌握Wor ...
- C# 调用word进程操作文档关闭进程
C# 调用word进程操作文档关闭进程 作者:Jesai 时间:2018-02-12 20:36:23 前言: office办公软件作为现在主流的一款办公软件,在我们的日常生活和日常工作里面几乎每天都 ...
- textContent、innerText的用法,在文档中插入纯文本
有时候需要查询纯文本形式的元素内容,或者在文档中插入纯文本.标准的方法是用Node的textContent属性来实现: var para = document.getElementsByTagName ...
- [翻译] DTCoreText 从HTML文档中创建富文本
DTCoreText 从HTML文档中创建富文本 https://github.com/Cocoanetics/DTCoreText 注意哦亲,DTRichTextEditor 这个组件是收费的,不贵 ...
- swagger2 导出离线Word/PDF/HTML文档
swagger2离线导出Word/PDF/HTML文档 1.前言 通过前面的两篇博客 我们已经介绍了如何使用spring boot整合swagger2 生成在线的API文档. 但是某些情况下,我们需要 ...
- python 解析docx文档的方法,以及利用Python从docx文档提取插入的文本对象和图片
首先安装docx模块,通过pip install docx或者在docx官方链接上下载安装都可以 下面来看下如何解析docx文档:文档格式如下 有3个部分组成 1 正文:text文档 2 一个表格. ...
随机推荐
- Python之命名空间、闭包、装饰器
一.命名空间 1. 命名空间 命名空间是一个字典,key是变量名(包括函数.模块.变量等),value是变量的值. 2. 命名空间的种类和查找顺序 - 局部命名空间:当前函数 - 全局命名空间:当前模 ...
- Android官方开发文档下载
Android官方开发文档 docs-24_r02.rar(链接:https://pan.baidu.com/s/12xC998JeUHj3ndfDXPM2ww 密码:bxyk) ADT下载.Andr ...
- zfs文件系统简单使用
关于ubuntu下zfs的使用参考:https://github.com/zfsonlinux/zfs/wiki/Ubuntu%2016.04%20Root%20on%20ZFS 安装zfs: 启动z ...
- 创建servlet的三种方式
第一种方式,实现Servlet接口 package com.example.servlet; import java.io.IOException; import javax.servlet.Serv ...
- Java(五、类和对象中的例题)
一.方法中的参数为数值型的(int) import java.util.Scanner; public class ScoreCalc { public void calc(int num1,int ...
- 修改ZendStudio新建php文件时的模板
zendstudio默认的模板不适用,可以自己到Window -- preferences -- php -- code style -- code templates -- code -- samp ...
- DX11 Without DirectX SDK--06 DirectXMath数学库
回到 DirectX11--使用Windows SDK来进行开发 xnamath.h原本是位于DirectX SDK的一个数学库,但是现在Windows SDK包含的数学库已经抛弃掉原来的xnamat ...
- 接口调用(发送http请求)
// 向对应的url地址发送http请求, 并获取响应的json字符串 public String getHttpResponse(String url) { // result用 ...
- Centos7 下 tty2等文字窗口的中文乱码问题分析
在使用 tty 的时候遇到了一个事情,那就是主文件夹下面的中文文件是乱码: [备注]tty 是 通过 CTRL + ALT +F2~F6 获得的, 这与桌面系统中的终端不是一个概念, 望看到这篇 ...
- idea运行多模块的maven项目,工作目录不一致的问题
我使用idea开发多模块的maven项目,目录结构如下: segment (父级) ---pom.xml ---core (子模块) ...