spire.doc 支持查找替换word中的文本、等。 前文介绍了如何用、替换word文本,本篇文章将介绍用图片、表格替换word文本的方法。
用图片替换word文本
测试文档:
c#
//实例化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);
vb.net
'实例化document类的对象,并加载测试文档
dim doc as document = new document
doc.loadfromfile("testfile.docx")
'加载替换的图片
dim image as image = image.fromfile("g.png")
'获取第一个section
dim sec as section = doc.sections(0)
'查找文档中的指定文本内容
dim selections() as textselection = doc.findallstring("google", true, true)
dim index as integer = 0
dim range as textrange = nothing
'遍历文档,移除文本内容,插入图片
for each selection as textselection in selections
dim pic as docpicture = 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)
next
'保存文档
doc.savetofile("result.docx", fileformat.docx)
替换结果:
用表格替换word文本
测试文档:
c#
//实例化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�cl2(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);
vb.net
'实例化document类的对象,并加载测试文档
dim doc as document = new document
doc.loadfromfile("test.docx")
'查找关键字符串文本
dim section as section = doc.sections(0)
dim selection as textselection = doc.findstring("参考附录", true, true)
'获取关键字符串所在段落的索引
dim range as textrange = selection.getasonerange
dim paragraph as paragraph = range.ownerparagraph
dim body as body = paragraph.ownertextbody
dim index as integer = body.childobjects.indexof(paragraph)
'添加一个两行三列的表格
dim table as 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�cl2(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)
替换结果: