需求:需要对现有文档在右下角插入图片
/// <summary>
/// 将图片插入到word中
/// </summary>
/// <param name="wordPath">被操作的源word文档</param>
/// <param name="picturePath">要插入的图片地址</param>
/// <param name="toWordPath">最后生成的新的word的存放位置</param>
void InsertPtctureToWord(string wordPath,string picturePath,string toWordPath)
{
Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word.Document doc = null;
try
{
object oMissing = System.Reflection.Missing.Value;
//图片地址
string fileName = picturePath;
object linkToFile = false;
object saveWithDocument = true;
app = new Microsoft.Office.Interop.Word.Application();
object docFileName = wordPath;
doc = app.Documents.Open(ref docFileName);
app.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;//进入页脚设置
#region 换一行
object _count = 1;
object WdLine = WdUnits.wdLine;
app.Selection.Move(ref WdLine, ref _count);
#endregion
app.Selection.TypeParagraph();//回车换行
object range = app.Selection.Range;//获得当前光标所在位置
Microsoft.Office.Interop.Word.InlineShape shape= app.Selection.InlineShapes.AddPicture(fileName, ref linkToFile, ref saveWithDocument, ref range);//插入图片
app.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;//将当前行右对齐
shape.Width = 100f;//设置图片宽度
shape.Height =20f;//设置图片高度
string physicNewFile = toWordPath;//生成的新文档位置
doc.SaveAs(physicNewFile);
}
catch (Exception ex)
{
}
finally
{
if (doc != null)
{
doc.Close();//关闭文档
}
if (app != null)
{
app.Quit();//退出应用程序
}
}
}
通过WdInformation枚举可以获得一些文档信息,参考地址:https://msdn.microsoft.com/zh-cn/library/ff837003.aspx;
http://www.cnblogs.com/koolay/articles/1398110.html里面有很多操作可以借鉴。