我尝试了许多不同的解决方案来尝试将图像插入到文档模板中。我已经取得了一些成功,但效果并不理想。基本上,我只是试图在文档的顶部插入一个图像,而不是其他所有内容。图像最多应该将其余内容沿页面向下移动,而不是在任何内容的顶部。
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
在这种情况下,start忽略模板边距,并将图像放在工作表的左边缘。即使我把它移到右边,让文本向下移动,它仍然不可取,因为顶部有太多的空白。
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)
'.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)
文档本身只是四个带有某种格式的表。我到底做错了什么?