在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")
效果图: