码迷,mamicode.com
首页 > Windows程序 > 详细

c# 操作word中在右下角插入图片

时间:2015-02-05 11:06:10      阅读:437      评论:0      收藏:0      [点我收藏+]

标签:

  需求:需要对现有文档在右下角插入图片

/// <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里面有很多操作可以借鉴。

c# 操作word中在右下角插入图片

标签:

原文地址:http://www.cnblogs.com/lvdong-1986/p/4274038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!