【发布时间】:2018-12-12 14:04:52
【问题描述】:
我尝试了多种不同的解决方案来尝试将图像插入到文档模板中。我取得了一些成功,但没有达到预期的效果。基本上,我只是想在所有其他内容之上的文档顶部插入图像。图片最多只能将其余内容移到页面下方,而不是放在任何内容之上。
With objDoc
Dim filePath As String = Path.Combine(appPath, fileName)
If Not filePath = "" Then
Dim img As Image = Image.FromFile(filePath)
Dim imgX As Integer = img.Width
Dim imgY As Integer = img.Height
'insert picture here
End If
End With
将上面的评论替换为以下内容,我已经成功了……有点。下面将图像插入到标题中。但是无论定义图像参数如何,图像都会以非常奇怪的方式缩放。
.PageSetup.DifferentFirstPageHeaderFooter = True
.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Shapes.AddPicture(
FileName:=filePath,
LinkToFile:=False,
SaveWithDocument:=True,
Left:=0,
Top:=0,
Width:=imgX,
Height:=imgY
).ConvertToInlineShape()
以下内容将在标题下方和文本上方插入图像。尝试将其转换为内联形状会引发异常。在这种情况下,给画布一个 0,0 开始会忽略模板边距并将图像放在工作表的左边缘。即使我将它向右移动并让文本向下移动,它仍然是不可取的,因为顶部有太多的空白。
Dim objCanvas As Word.Shape = objWordApp.ActiveDocument.Shapes.AddCanvas(Left:=0, Top:=0, Width:=imgX, Height:=imgY)
objCanvas.CanvasItems.AddPicture(FileName:=filePath, LinkToFile:=False, SaveWithDocument:=True)
以下其余部分是我通过 msdn 和其他各种渠道进行筛选的尝试。这些都没有显示任何图像。
'.InlineShapes.AddPicture(filePath, Type.Missing, Type.Missing, Type.Missing)
'Dim objRng As Word.Range = .Range()
'objRng.InlineShapes.AddPicture(filePath)
'Dim objInlineShape As Word.InlineShape = objWordApp.Selection.InlineShapes.AddPicture(
' FileName:=filePath,
' LinkToFile:=False,
' SaveWithDocument:=True
')
'objInlineShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue
'objInlineShape.Width = imgX
'objInlineShape.Height = imgY
'.Application.Selection.InlineShapes.AddPicture(filePath)
'Dim objRng As Word.Range = .Sections(1).Range()
'objRng.InlineShapes.AddPicture(filePath)
'
'Dim objInlineShape As Word.InlineShape = .InlineShapes.AddPicture(filePath)
'Dim objShape As Word.Shape = objInlineShape.ConvertToShape()
'objDoc.Bookmarks.Item("\startofdoc").Range.InlineShapes.AddPicture(filePath)
文档本身只有四个带有一些格式的表格。我到底做错了什么?
【问题讨论】:
-
objDoc.Sections(1).Headers(1).Shapes.AddPicture(filePath, False, True, Width:=imgX, Height:=imgY)为我工作
标签: vb.net ms-word office-interop