C# 替换Word文本—— 用文档、图片、表格替换
2018-11-28 14:33
204 查看
编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改。在本篇文章中,将介绍如何用一篇Word文档、图片或者表格来替换文档中的指定文本字符串。示例要点如下:
- 用文档替换Word中的文本
- 用图片替换Word中的文本
- 用表格替换Word中的文本
工具
- Free Spire.Doc for .NET
下载安装后,注意在程序中添加引用Spire.Doc.dll(如下图),dll文件可在安装路径下的Bin文件夹中获取。
C#代码示例
【示例1】用文档替换Word中的文本
测试文档:
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中的文本
测试文档:
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中的文本
测试文档:
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中的文本字符串的”的全部内容。
(本文完)
相关文章推荐
- C# 替换Word文本—— 用文档、图片、表格替换文本
- C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化
- C#打开WORD文档内容并显示(未完善表格图片等排版显示的处理)
- 利用docx4j完美导出word文档(标签替换、插入图片、生成表格)
- poi操作word文档,以07版本为参考,将word文档中图片替换为文本(替换文本也一样),读图片可选文字内容
- C#操作Word总结(二)——设置文档格式并添加文本内容、超链接、添加图片
- C# 替换Word文档中的图片
- C# 为 Word 文档添加文本和图片水印
- 提取word文档的图片及表格并用html代码替换
- C#(MVC) Word 替换,填充表格,导出并下载PDF文档
- 用C#操作office组件库实现word文档的创建及文字 表格 图片的插入
- c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
- c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- 使用C#替换Word文档里的文字和图片
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- C# 操作Word书签(二)——插入图片、表格到书签;读取、替换书签
- C#打开WORD文档内容并显示(未完善表格图片等排版显示的处理)
- Java处理word文档 用jacob 表格图片文字替换
- C#操作office之word文本替换,只能实现替换其中一个,可以替换超长文本