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

Word VSTO (VB.NET) - Get the image location, size, etc,?

我正在使用 VB.NET VSTO for 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

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

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

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

    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)

您还可以获得图像的顶部和左侧位置(尽管它可能对您没有任何好处,具体取决于您想要它的原因)。 get_Information方法还有wdHorizontalPositionRelativeToPagewdVerticalPositionRelativeToPage