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[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

时间: 2024-08-27 17:50:03

C# 替换Word文本—— 用文档、图片、表格替换文本的相关文章

替换WORD/EXCEL模板文档中的内容并下载java

import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Iterator; import java

桌面支持--word文档图片显示乱码1

word文档图片显示乱码1

使用NOPI读取Word、Excel文档内容

使用NOPI读取Excel的例子很多,读取Word的例子不多. Excel的解析方式有多中,可以使用ODBC查询,把Excel作为一个数据集对待.也可以使用文档结构模型的方式进行解析,即解析Workbook(工作簿).Sheet.Row.Column. Word的解析比较复杂,因为Word的文档结构模型定义较为复杂.解析Word或者Excel,关键是理解Word.Excel的文档对象模型. Word.Excel文档对象模型的解析,可以通过COM接口调用,此类方式使用较广.(可以录制宏代码,然后替

word 2013 长篇文档排版案例教程

1 前言 平时偶尔可能会遇到需要制作长篇文档的情况,比如论文.书稿等,需要进行一些复杂的编辑,像设置级别标题.分节显示页码.封面.标题.目录.页眉.页脚.参考文献等等,不少同学为此犯愁,特别是非计算机专业的朋友们经常向我求救.其实,早在十几年前微软就曾经发布一个基于Office 2003长篇文档制作的视频教程,我就是从那个视频里学习了一些复杂的排版方法.现在,Office已经发展到2013版了,也没有发现微软再发新的教程.其实大体的流程和原理与Office 2003版的类似.我觉得很有必要把相关

怎样实现pdf与word等普通文档的转换

Adobe的PDF文档的转换可能是大家比较关心的话题.尤其是PDF转成Word的DOC格式,或者把Word的DOC文档转换成PDF文档运用地最为广泛.或许大家并不清楚,除了DCOE与PDF格式转换外,很多时候我们还会进行其他文档的处理.此时,你需要的是一款多功能集于一体的PDF文件转换器.问题随之而来.什么样的PDF转换成Word转换器可以满足大家办公文档转换的需求呢? 迅捷PDF转换器支持多种文档格式,DOC.XLS.PPT.TXT.HTML.图片等等.要把他们相互转换,可以用借助这款软件特有

C#采用OpenXml给Word文档添加表格

本文实例讲述了C#采用OpenXml给Word文档添加表格的方法,是非常实用的操作技巧.分享给大家供大家参考.具体分析如下: 这里将展示如何使用Openxml向Word添加表格. 代码中表头和数据我们用的同一个TableRow来添加,其实可以通过TableHeader来,其实都一样.后面我们还会进一步给出如何设置单元格样式.表头那一行可以自己通过设置样式来控制 示例代码如下: using System; using System.Collections.Generic; using System

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

DTCoreText 从HTML文档中创建富文本 https://github.com/Cocoanetics/DTCoreText 注意哦亲,DTRichTextEditor 这个组件是收费的,不贵,才650美元而已^_^. DTCoreText This project aims to duplicate the methods present on Mac OSX which allow creation of NSAttributedString from HTML code on iO

AOPR破解Office Word和Excel文档密码有风险吗?

Advanced Office Password Recovery作为一款专业的Office密码破解软件,支持的破解文件格式齐全,从Office2.0到2013版本.可破解的密码类型众多,从常设的打开密码到鲜有的VBA程序密码.所以,AOPR破解Office 97/2000兼容格式的Word和Excel文档密码,根本就是小菜一碟. AOPR与AOPB的破解效果 当需要破解密码的Word或Excel文档以Office 97/2000兼容格式或优于Office 97/2000格式保存时,Advanc

php实现word转html文档的例子

php实现word转html文档的例子 2015-10-16 17:07 2751人阅读 评论(2) 收藏 举报 word文档不适合放到网页上了,如果我们要放到网页中去是需要一个个复制了,如果你还在复制就out了,下文小编来为各位整理一篇php实现word转html文档的例子,希望文章对各位有帮助. 要想完美解决,office转pdf或者html,最好还是用windows office软件,libreoffice不能完美转换,wps没有api. 先确认com模块是不是开启,phpinfo里面如果