在word中我们可以对插入到文档中的图片的大小进行调整,使得图片与文章更加协调、美观。本文将介绍如何使用spire.doc和c#来调整word文档中图片的大小。
以下是我们所使用的示例文档的截图:
c#
//加载word文档
document document = new document("input.docx");
//获取第一个section
section section = document.sections[0];
//获取第一个段落
paragraph paragraph = section.paragraphs[0];
//调整段落中图片的大小
foreach (documentobject docobj in paragraph.childobjects)
{
if (docobj is docpicture)
{
docpicture picture = docobj as docpicture;
picture.width = 50f;
picture.height = 50f;
}
}
//保存文档
document.savetofile("resizeimages.docx");
vb.net
'加载word文档
dim document as document = new document("input.docx")
'获取第一个section
dim section as section = document.sections(0)
'获取第一个段落
dim paragraph as paragraph = section.paragraphs(0)
'调整段落中图片的大小
for each docobj as documentobject in paragraph.childobjects
if (typeof docobj is docpicture) then
dim picture as docpicture = ctype(docobj,docpicture)
picture.width = 50!
picture.height = 50!
end if
next
'保存文档
document.savetofile("resizeimages.docx")
效果图: