2

下面是 spire.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.docMicrosoft.Office.Interop.Word库将图像和文本插入 Word 模板中的某些位置?

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

4

2 回答 2

0

使用 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

希望能帮助到你。

于 2018-09-10T12:51:35.930 回答