C# 在word文档指定位置插入图片

需求:

        需要在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);
                }

             }

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值