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

时间:2013-11-13 14:05:00

标签: c# spire.doc

使用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模板中的某些位置?

或类似于此link,其中插入图片到世界模板中的某个位置。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分: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

希望有帮助。

相关问题