本篇文章将介绍如何使用spire.doc for .net来实现将图片插入到word批注以及提取word批注中的图片。
插入图片到word批注
c#
//创建document类的对象,加载文档
document doc = new document();
doc.loadfromfile("testfile.docx");
//获取第一个批注
comment comment = doc.comments[0];
//实例化docpicture类,加载图片
docpicture docpicture = new docpicture(doc);
image img = image.fromfile("img1.png");
docpicture.loadimage(img);
//插入图片到批注
comment.body.addparagraph().childobjects.add(docpicture);
//保存文档
doc.savetofile("result.docx", fileformat.docx2013);
vb.net
'创建document类的对象,加载文档
dim doc as document = new document
doc.loadfromfile("testfile.docx")
'获取第一个批注
dim comment as comment = doc.comments(0)
'实例化docpicture类,加载图片
dim docpicture as docpicture = new docpicture(doc)
dim img as image = image.fromfile("img1.png")
docpicture.loadimage(img)
'插入图片到批注
comment.body.addparagraph.childobjects.add(docpicture)
'保存文档
doc.savetofile("result.docx", fileformat.docx2013)
图片插入效果:
读取word批注中的图片
c#
//创建实例,加载文档
document doc = new document();
doc.loadfromfile("sample.docx");
int index = 0;
//遍历批注,获取图片
foreach (comment comment in doc.comments)
{
foreach (paragraph p in comment.body.paragraphs)
{
foreach (documentobject docobject in p.childobjects)
{
if (docobject.documentobjecttype == documentobjecttype.picture)
{
docpicture picture = docobject as docpicture;
string imagename = string.format("图片{0}.png", index);
picture.image.save(imagename, system.drawing.imaging.imageformat.png);
index ;
}
}
}
}
vb.net
'创建实例,加载文档
dim doc as document = new document
doc.loadfromfile("sample.docx")
dim index as integer = 0
'遍历批注,获取图片
for each comment as comment in doc.comments
for each p as paragraph in comment.body.paragraphs
for each docobject as documentobject in p.childobjects
if (docobject.documentobjecttype = documentobjecttype.picture) then
dim picture as docpicture = ctype(docobject,docpicture)
dim imagename as string = string.format("图片{0}.png", index)
picture.image.save(imagename, system.drawing.imaging.imageformat.png)
index = (index 1)
end if
next
next
next
图片读取结果: