首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将图像和文本插入到Microsoft Word模板中?

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

Stack Overflow用户
提问于 2013-11-13 22:05:00
回答 1查看 13.3K关注 0票数 2

下面是使用spire.doc的代码

代码语言:javascript
复制
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模板中的某些位置?

或者类似于将图像插入到world模板中的某个位置的link

EN

回答 1

Stack Overflow用户

发布于 2018-09-10 20:51:36

使用Microsoft.Office.Interop.Word,

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

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19955812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档