不妨试试免费版的Spire.Doc, 下面的代码说如何获取Word文档中的图片,对将图片设置为垂直居中
下载地址:http://www.e-iceblue.cn/Downloads/Free-Spire-Doc-NET.html
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace test
{
class Program
{
static void Main(string[] args)
{
//加载一个Word文档
Document document = new Document();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\Image.docx");
//创建List对象来存储获取的图片
List<DocPicture> list = new List<DocPicture>();
//遍历文档中的section
foreach (Section section in document.Sections)
{
//遍历section中的段落
foreach (Paragraph paragraph in section.Paragraphs)
{
//遍历段落中的子对象
foreach (DocumentObject docObject in paragraph.ChildObjects)
{
//判断子对象是否是图片
if (docObject.DocumentObjectType == DocumentObjectType.Picture)
{
//将图片存入List
DocPicture picture = docObject as DocPicture;
list.Add(picture);
}
}
}
}
//将获取到的第一张图片的文字环绕方式设置为文字上方
list[0].TextWrappingStyle = TextWrappingStyle.InFrontOfText;
//设置第一张图片相对于整个页面垂直居中
list[0].VerticalAlignment = ShapeVerticalAlignment.Center;
list[0].VerticalOrigin = VerticalOrigin.Page;
//设置第一张图片水平也居中
list[0].HorizontalAlignment = ShapeHorizontalAlignment.Center;
//保存到文档
document.SaveToFile("output.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("output.docx");
}
}
}