Word VSTO(VB.NET) - 获取图像的位置,大小等,?

时间:2015-06-20 23:54:37

标签: vb.net image ms-word location vsto

我正在使用VB.NET VSTO为MS-Word创建一个加载项。在这个程序中,我需要检索文档上所有图像的位置(左侧,顶部)和大小(高度,重量)等详细信息。我还想检索图像所在的页码。我使用以下代码,

        Dim i As Integer
        For i = 1 To Globals.ThisAddIn.Application.ActiveDocument.InlineShapes.Count
            If Globals.ThisAddIn.Application.ActiveDocument.InlineShapes(i).Type = Word.WdInlineShapeType.wdInlineShapePicture Then
                strHeight = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes(i).ScaleHeight()
                strWidth = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes(i).ScaleWidth()
            End If
        Next i

但是,这只能检索尺寸(高度,重量)。如何获取图像的位置(左,上)和页码?

2 个答案:

答案 0 :(得分:2)

就其本质而言,InlineShape没有可定位的顶部和左侧。它就是内联,这意味着它存在于文档的文本层中,并且位置根据文本和/或其前面的其他内容浮动。如果该项目在第2页上,并且您在InlineShape(i)之前插入了25行文本或其他图片,则所述形状将向下浮动到第3页(或4或其他任何内容。)

只需使用.Height和.Width即可访问高度和宽度。 ScaleHeight和ScaleWidth是反映文档中对象相对于对象原始大小的大小的属性。但是,您可能希望将高度和宽度存储为字符串,因为该属性返回单个(数字)值。高度和宽度:

    Dim i As Integer
    Dim shp as InlineShape
    For i = 1 To Globals.ThisAddIn.Application.ActiveDocument.InlineShapes.Count
        shp = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes(i)
        If shp.Type = Word.WdInlineShapeType.wdInlineShapePicture Then
            strHeight = shp.Height.ToString()
            strWidth = shp.Width.ToString()
        End If
    Next i

要获取页码,您必须引用InlineShape的范围。

    shp.Range.get_Information(Word.WdInformation.wdActiveEndPageNumber)

答案 1 :(得分:1)

您还可以获得图像的顶部和左侧位置(虽然它可能对您没有任何好处,具体取决于您想要它的原因)。 get_Information方法还有dist-packages/django/forms/models.pydefault=new_key

相关问题