如何将图像和文本插入Microsoft Word 模板



下面有尖顶的代码.doc

var doc = new Document();
doc.LoadFromFile(@"E:test.docx", FileFormat.Doc);
var image = Image.FromFile(@"E:nice.jpg");
var picture1 = doc.Sections[0].Paragraphs[0].AppendPicture(image);
picture1.VerticalAlignment = ShapeVerticalAlignment.Top;
picture1.HorizontalAlignment = ShapeHorizontalAlignment.Left;
picture1.TextWrappingStyle = TextWrappingStyle.Square;
doc.SaveToFile(@"....result.doc", FileFormat.Doc);
System.Diagnostics.Process.Start(@"....result.doc");

如何使用spire.doc库或Microsoft.Office.Interop.Word库将图像和文本插入 Word 模板中的某些位置?

或与此链接类似,将图像插入世界模板中的某个位置。

  • 使用互操作 C# 在 Word 文档中最后一个图像/文本之后添加图像
  • 如何将图片插入Word文档后更改图片的大小
  • 更多在这里

使用 Microsoft.Office.Interop.Word,

首先,您需要确保选择的位置正确,

然后插入图像,有几个选项,这是我的代码,

If bInline Then
    Dim oInlineShape As InlineShape = m_oWordApp.Selection.InlineShapes.AddPicture(FileName:=sImageFilePath, LinkToFile:=False, SaveWithDocument:=True)
oInlineShape.Range.ParagraphFormat.Alignment = nAlignment
    If nHeight > 0 AndAlso nWidth > 0 Then
        oInlineShape.LockAspectRatio = MsoTriState.msoFalse
        oInlineShape.Height = nHeight
        oInlineShape.Width = nWidth
    End If
Else
    Dim oShape As Word.Shape = m_oWordApp.ActiveDocument.Shapes.AddPicture(Anchor:=m_oWordApp.Selection.Range, FileName:=sImageFilePath, LinkToFile:=False, SaveWithDocument:=True)
    If nHeight > 0 AndAlso nWidth > 0 Then
        oShape.LockAspectRatio = MsoTriState.msoFalse
        oShape.Height = nHeight
        oShape.Width = nWidth
    End If
End If

插入文本非常简单,您只需要确保选择对象位于正确的位置,然后,

m_oWordApp.Selection.Text = sMsg
'you can update background colors ....
m_oWordApp.Selection.Range.HighlightColorIndex = 0
'you update fonts ....
m_oWordApp.Selection.Font.Bold = True

希望对您有所帮助。

最新更新