概述
需求:
需要在word文档中固定位置插入,公章;
方法一:
object filename = filePath; //要打开的文档路径
string strKey = strSearch; //要搜索的文本
object MissingValue = Type.Missing;
int i = 0, iCount = 0;
try
{
wp = new Word.ApplicationClass();
wd = wp.Documents.Open(ref filename, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue);
if (wd.Paragraphs != null && wd.Paragraphs.Count > 0)
{
iCount = wd.Paragraphs.Count;
for (i = iCount; i >= 0; i--) //遍历查找
{
wfnd = wd.Paragraphs[i].Range.Find;
wfnd.ClearFormatting();
wfnd.Text = strKey;
if (wfnd.Execute(ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue))
{
object Anchor = wd.Paragraphs[i].Range; //记录查找到的字符串位置
object LinkToFile = false;
object SaveWithDocument = true;
Microsoft.Office.Interop.Word.InlineShape Inlineshape = wp.Selection.InlineShapes.AddPicture(
ImagPath, ref LinkToFile, ref SaveWithDocument, ref Anchor);
Inlineshape.Select(); //这一步很重要 不然后面转换会报错
Word.Shape shape = Inlineshape.ConvertToShape();
shape.WrapFormat.Type = Word.WdWrapType.wdWrapFront; //图片插入类型设置为 在为本上
shape.Top = 675; //设置在本页插入图片的位置(不同的文本要多试几次)
shape.Left = 395;
isFindSealLoc = true;
break;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally //文档一定要释放,不然内存回常驻大量副本,而且会在文件夹中参数大量隐藏文件
{
if (wd != null)
{
wd.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wd);
wd = null;
}
if (wp != null)
{
wp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wp);
wp = null;
}
}
方法二:
object filename = filePath;
string ImagePath = ImagPath;
string strKey = strSearch;
object MissingValue = Type.Missing;
bool isFindSealLoc = false;
Word.Application wp = null;
Word.Document wd = null;
try
{
wp = new Word.ApplicationClass();
wd = wp.Documents.Open(ref filename, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue);
wp.Selection.Find.ClearFormatting();
wp.Selection.Find.Replacement.ClearFormatting();
wp.Selection.Find.Text = strKey;
object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
if (wp.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue,
ref MissingValue, ref objReplace, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue))
{
object Anchor = wp.Selection.Range;
object LinkToFile = false;
object SaveWithDocument = true;
Microsoft.Office.Interop.Word.InlineShape Inlineshape = wp.Selection.InlineShapes.AddPicture(
ImagePath, ref LinkToFile, ref SaveWithDocument, ref Anchor);
Inlineshape.Select();
Word.Shape shape = Inlineshape.ConvertToShape();
shape.WrapFormat.Type = Word.WdWrapType.wdWrapFront;
shape.Top = 675;
shape.Left = 395;
isFindSealLoc = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (wd != null)
{
wd.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wd);
wd = null;
}
if (wp != null)
{
wp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wp);
wp = null;
}
}
重点:
程序由于执行不成功或word文件异常退出会产生大量临时隐藏文件处理方法
FolderBrowserDialog FBDig = new FolderBrowserDialog();
if (FBDig.ShowDialog() == DialogResult.OK)
{
readFileNum = 0;
handleFileNum = 0;
handleFailfileName_list.Clear();
string[] _files = Directory.GetFiles(FBDig.SelectedPath, "*.doc");
List<string> filePathlist = new List<string>();
foreach(string _file in _files)//清楚word文档非正常退出时候,在文件夹中产生的临时文件
{
if (_file.Contains("~$"))
continue;
filePathlist.Add(_file);
}
}
最后
以上就是鲤鱼小土豆为你收集整理的C# 在word文档指定位置插入图片的全部内容,希望文章能够帮你解决C# 在word文档指定位置插入图片所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复