当前位置: 移动技术网 > IT编程>开发语言>.net > C# 替换Word文本—— 用文档、图片、表格替换文本

C# 替换Word文本—— 用文档、图片、表格替换文本

2018年11月29日  | 移动技术网IT编程  | 我要评论

罗通拜师,案件聚焦2011,伦敦银走势图

编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改。在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");
        }
    }
}
view code

 

 

【示例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");
        }
    }
}
view code

 

【示例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");
             
        }
    }
}
view code

 

以上是本次关于“c# 用文档、图片、表格替换word中的文本字符串的”的全部内容。

(本文完)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网