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%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);
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%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)
替换结果: