我想在文档的每个页面的右上角放置一个徽标。此功能已存在于由我们管理的Word加载项中。但是此功能无法正常工作。加载项将图像转换为形状,然后将此图像放置在距左侧文档角的固定距离处。这适用于A4格式的文档,但只要文档的方向或大小发生变化,徽标位置就会关闭。
我已经尝试了很多策略来解决这个问题,但是没有找到令人满意的方法。我当前的策略是动态确定左页面和徽标之间的距离,然后通过调用.RelativeHorizontalPosition属性并将其链接到右边距区域,使该位置相对于页面的右侧。
不幸的是,与Shape对象的.Left属性进行交互很麻烦。 .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
答案 0 :(得分:1)
不是将左侧位置设置为绝对值,而是将其设置为相对且基本上“右对齐”形状。如果如下所示设置RelativeHorizontalPosition和Left属性,则图像将放置在右上角,即使文档的格式或大小发生更改,也会保持其与该角的相对位置。
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
当然,如果标题中还有其他项目,那么您将创建一个包含多个单元格的表格并适当调整间距,但对于此示例,我只是在标题中放置一个无边框单单元格表并设置其自动调整行为以适应窗口,以便即使更改边距或格式,表也将填充页面的宽度。然后,我只需使用图像设置单元格的顶部和右侧填充,即可实现您正在寻找的行为。