编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改。在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文本—— 用文档、图片、表格替换文本的更多相关文章

  1. PictureCleaner 官方版 v1.1.3.04061,免费的图片校正及漂白专业工具,专业去除文档图片黑底麻点杂色,规格化A4、B5尺寸输出,还你一个清晰的文本。

    当家长多年,每天都要拍照试卷打印.用App去掉图片黑底就成了每天必备工作.可是,有些图片文件不是来自手机,所以需要一个电脑版的图片漂白工具.经过一个多月努力,PictureCleaner官方版诞生了 ...

  2. 基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理

    http://www.cnblogs.com/wuhuacong/p/4175266.html 在一般的管理系统模块里面,越来越多的设计到一些常用文档的上传保存操作,其中如PDF.Word.Excel ...

  3. Aspose.Words操作word生成PDF文档

    Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...

  4. 用R创建Word和PowerPoint文档--转载

    https://www.jianshu.com/p/7df62865c3ed Rapp --简书 Microsoft的Office软件在办公软件领域占有绝对的主导地位,几乎每个职场人士都必须掌握Wor ...

  5. C# 调用word进程操作文档关闭进程

    C# 调用word进程操作文档关闭进程 作者:Jesai 时间:2018-02-12 20:36:23 前言: office办公软件作为现在主流的一款办公软件,在我们的日常生活和日常工作里面几乎每天都 ...

  6. textContent、innerText的用法,在文档中插入纯文本

    有时候需要查询纯文本形式的元素内容,或者在文档中插入纯文本.标准的方法是用Node的textContent属性来实现: var para = document.getElementsByTagName ...

  7. [翻译] DTCoreText 从HTML文档中创建富文本

    DTCoreText 从HTML文档中创建富文本 https://github.com/Cocoanetics/DTCoreText 注意哦亲,DTRichTextEditor 这个组件是收费的,不贵 ...

  8. swagger2 导出离线Word/PDF/HTML文档

    swagger2离线导出Word/PDF/HTML文档 1.前言 通过前面的两篇博客 我们已经介绍了如何使用spring boot整合swagger2 生成在线的API文档. 但是某些情况下,我们需要 ...

  9. python 解析docx文档的方法,以及利用Python从docx文档提取插入的文本对象和图片

    首先安装docx模块,通过pip install docx或者在docx官方链接上下载安装都可以 下面来看下如何解析docx文档:文档格式如下 有3个部分组成 1 正文:text文档 2 一个表格. ...

随机推荐

  1. tomcat启动报错:Address already in use: JVM_Bind

    tomcat启动时出现Address already in use: JVM_Bind 的原因是因为端口被占用,有可能是因为多次启动tomcat或者启动了多个tomcat,或者是其他应用程序或者服务占 ...

  2. python new和init知识点

    __new__ 方法是什么?如果将类比喻为工厂,那么__init__()方法则是该工厂的生产工人,__init__()方法接受的初始化参 数则是生产所需原料,__init__()方法会按照方法中的语句 ...

  3. backbone的一些认识

    body,td { font-family: 微软雅黑; font-size: 10pt } 官网:http://backbonejs.org/ 作者:Jeremy Ashkenas 杰里米·阿什肯纳 ...

  4. spring-boot-oracle spring-batch

    Install/Configure Oracle express Oracle xe installer for linux (I don't care if you're running linux ...

  5. mybatis整合spring获取配置文件信息出错

    描述:mybatis整合spring加载jdbc.properties文件,然后使用里面配置的值来 配置数据源,后来发现用户变成了admin- jdbc.properties的配置: 加载配置: 报错 ...

  6. java基础之接口(抽象类与接口的区别)

    概述 猫狗案例,我们想想狗一般就是看门,猫一般就是作为宠物了,对不.但是,现在有很多的驯养员或者是驯的,这应该属于经过特殊的培训训练出来的,对不.所以,这些额外的动作定义到动物类中就不合适,也不适合直 ...

  7. 在线OJ使用总结(acm)

    赛码网OJ规则 用readLine()代替read_line() 用readLine()代替read_line() 用readLine()代替read_line() 用readLine()代替read ...

  8. 使用nginx搭建https服务器

    http://www.cnblogs.com/tintin1926/archive/2012/07/12/2587311.html 最近在研究nginx,整好遇到一个需求就是希望服务器与客户端之间传输 ...

  9. MySQL数据库简识

    MySQL:关系型数据库      (由瑞典MySQL AB公司开发,后来被Sun公司收购,Sun公司后来又被Oracle公司收购,目前属于Oracle旗下产品)         开源 免费 不要钱 ...

  10. Selenium 指定浏览器位置

    在脚本开头要指定浏览器位置. public static void main(String[] args) throws InterruptedException, IOException { Sys ...