我想在文档中的每个页面上放置一个徽标。该功能已经存在于我们管理的单词加载项中。但是,此功能无法正常工作。加载项将图像转换为形状,然后将该图像距左文档角的固定距离。这适用于A4格式的文档,但是每当文档的方向或大小更改时,徽标放置就会关闭。
我尝试了许多策略来解决此问题,但还没有找到令人满意的方法。我当前的策略是通过调用.RelativeHorizontalPosition属性并将其链接到右边的边距区域,以动态地确定左页侧和徽标之间的距离,然后将此位置相对于页面的右侧。
不幸的是,与形状对象的左侧属性进行交互是麻烦的。.LEFT属性没有考虑我分配的值,而是承担负值。我已经检查了多次分配的参数。有人知道为什么是这种情况以及如何解决吗?
示例代码
Private Sub AddLogos(section As Section, header As HeaderFooter)
Dim wordApp As Word.Application = Globals.ThisAddIn.Application
Dim pageWidth As Single = section.PageSetup.PageWidth
Dim imgFilePath As String = "filepath"
Dim leftDistanceA4 As Single = 11
Dim logo As Word.Shape
Try
If wordApp.ActiveDocument.SaveFormat >= 12 Then
logo = header.Range.InlineShapes.AddPicture(m_sImageLogo, False, True).ConvertToShape()
Else 'Word 97-2003 Support
logo = header.Shapes.AddPicture(imgFilePath, False, True)
End If
Catch ex As Exception
Throw New Exception("Error message.")
End Try
Dim distanceFromRightPageEdge = wordApp.CentimetersToPoints(21 - leftDistanceA4)
Dim distanceFromLeftPageEdge = pageWidth - distanceFromRightPageEdge
With logo
.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage
.Left = distanceFromLeftPageEdge
.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionRightMarginArea
End With
,而不是将左位置设置为绝对值,您可以将其相对相对,从本质上"右对齐"形状。如果您设置了相对的HorizontalPosition和左属性,如下所示,该图像将放置在右上角,即使更改了文档的格式或大小,也将保持其相对位置。
Const imgpath As String = "[your path]"
Dim app As New Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document = app.Documents.Add()
Dim head As Microsoft.Office.Interop.Word.HeaderFooter = doc.Sections(1).Headers(1)
Dim img As Microsoft.Office.Interop.Word.Shape = head.Shapes.AddPicture(imgpath, False, True)
With img
.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
.Left = Microsoft.Office.Interop.Word.WdShapePosition.wdShapeRight
End With
app.Visible = True
'dispose references
编辑:如果您需要对定位的更多控制,而不是简单地将图像锚定在页面的右上角,则内联形状并不固有地拥有。相反,请考虑使用标题中的无边界表来提供对其内容的更多控制。一旦图像是表格的孩子,您就可以访问所有表格格式控件以在图像上使用:
Const imgpath As String = "[your path]"
Const imgMarginCM As Integer = 2
Dim app As New Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document = app.Documents.Add()
Dim head As Microsoft.Office.Interop.Word.HeaderFooter = doc.Sections(1).Headers(1)
Dim tbl As Microsoft.Office.Interop.Word.Table = doc.Tables.Add(head.Range, 1, 1)
With tbl
.Borders.Enable = False
.AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitWindow)
.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight
.Cell(1, 1).TopPadding = app.CentimetersToPoints(imgMarginCM)
.Cell(1, 1).RightPadding = app.CentimetersToPoints(imgMarginCM)
.Cell(1, 1).Range.InlineShapes.AddPicture(imgpath, False, True)
End With
app.Visible = True
'dispose references
当然,如果您的标题中有其他项目自动对fitwindow的行为,以便即使更改边距或格式,表也将填充页面的宽度。然后,我只是用图像设置了单元格的顶部和右填充,并且您要寻找的行为。