我想请问用过Spire.Doc的大神们,在word中一个文本替换成一个图片,里面有什么方法吗?

网上都搜不到类似的,这个需求应该很普通啊,请赐教!!!
读取一个模板,将模板里面的图片或者问题替换成自己想需要的东西,这种需求再普通不过了,怎么Spire.Doc只有Replace文字的,没有说替换图片呢?

可以把Word中的文本替换成图片呢,试试下面的代码

//实例化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);

出自 ç½‘页链接

温馨提示:答案为网友推荐,仅供参考
相似回答