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 一个表格. ...
随机推荐
- React Native系列(6) - 编译安卓私有React-Native代码
为何要自己编译React Native安卓私有代码 我们在开发中遇到一个HTTP2的问题,React Native安卓客户端在和HTTP2支持的服务器通讯的过程中会有crash,见 React-Nat ...
- tkinter简介(一)
Tkinter(也叫 Tk 接口)是 Tk 图形用户界面工具包标准 的 Python 接口.Tk 是一个轻量级的跨平台图形用户界面 (GUI)开发工具. Tk 和 Tkinter 可以运行在大多数 的 ...
- 超实用的JavaScript代码段 Item5 --图片滑动效果实现
先上图 鼠标滑过那张图,显示完整的哪张图,移除则复位: 简单的CSS加JS操作DOM实现: <!doctype html> <html> <head> <me ...
- VS2012中出现“无法启动程序...debug\abc.exe,系统找不到指定文件”的问题!
VS 2005在生成可执行文件时使用了一种新的技术,该技术生成的可执行文件会伴随生成一个清单文件(manifest file)(.manifest后缀文件)(其本质上是XML文档,你可以用文本编辑器打 ...
- Android 实现形态各异的双向侧滑菜单 自定义控件来袭
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39670935,本文出自:[张鸿洋的博客] 1.概述 关于自定义控件侧滑已经写了两 ...
- selenium+java破解极验滑动验证码的示例代码
转自: https://www.jianshu.com/p/1466f1ba3275 selenium+java破解极验滑动验证码 卧颜沉默 关注 2017.08.15 20:07* 字数 3085 ...
- java中&和&& | 和||的区别
我想很多人在学习java的时候,或者其他语言(如:C#,.Net等)都会遇到 &和&& 然而,如果你没有真正的理解他们的意思,这会给你思路上面带来很大的麻烦 在这篇blog中, ...
- bzoj3236 作业 莫队+树状数组
莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- 如何改变XCode的默认设置
改变bundle ID 进入 /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Appl ...
- 正则表达式(Regular expressions)使用笔记
Regular expressions are a powerful language for matching text patterns. This page gives a basic intr ...