C#操作word文档,通过书签插入文字、图片和复选框

word文档模板中在需要插入的地方创建书签

 private void PreviewFun(object p)
        {
            CheckBackData row = DataTableControl.SelectFirstObject as CheckBackData;
            if (row.IsConfirm == 0)
            {
                SystemService.MsgBoxService.ShowMsgBox("还未生成证书,请先确认");
                return;
            }
			
			//将服务器的文件下载到serviceTemplatPath路径下
            string fileName = "仪器退回通知书.doc";
            //项目下的debug的temp路径
            string path = TempFileAccess.TempDir;
            var serviceTemplatPath = path + fileName;
            string date = FileFun(fileName);
            if (File.Exists(serviceTemplatPath))
            {
               File.Delete(serviceTemplatPath);
            }
            Model.HttpUpLoadSerivce.DownLoadUploadFile("quotation", date, fileName, serviceTemplatPath, null);
            if (!File.Exists(serviceTemplatPath))
            {
                DataTableControl.AsynUi.PrintTitleMessage("模板不存在", "信息提示");
                return;
            }

			//打开对话框选择路径
            //System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            //fbd.ShowDialog();
            //if (fbd.SelectedPath == string.Empty)
            //{
            //    DataTableControl.AsynUi.PrintTitleMessage("请选择导出位置", "信息提示");
            //    return;
            //}
            //var cachePath = fbd.SelectedPath + "\\" + row.CheckBackId + fileName;
            //Random random = new Random();
            //if (File.Exists(cachePath))
            //{
            //    cachePath = fbd.SelectedPath + "\\" + row.CheckBackId + random.Next(100000, 999999) + fileName;
            //}
            //项目下的debug下的cache路径
            var cachePath = Directory.GetCurrentDirectory() + "\\cache\\" + row.CheckBackId + fileName;
            Random random = new Random();
            if (File.Exists(cachePath))
            {
                cachePath = Directory.GetCurrentDirectory() + "\\cache\\" + random.Next(100000, 999999) + fileName;
            }
            File.Copy(serviceTemplatPath, cachePath);  //将下载的文件copy到缓存下

            Word.Application application = new Word.Application();
            Word.Document doc = new Word.Document();
            object Obj_FileName = cachePath;
            object Visible = false;
            object ReadOnly = false;
            object missing = System.Reflection.Missing.Value;
            //打开文件
            doc = application.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref Visible,
                ref missing, ref missing, ref missing,
                ref missing);
            doc.Activate();

            var table = doc.Tables[1];
            table.Cell(1, 2).Range.Text = row.TaskNo;
            table.Cell(1, 4).Range.Text = row.DeptName;
            table.Cell(2, 2).Range.Text = row.DelegateOrgName;
            table.Cell(3, 2).Range.Text = row.Name;
            table.Cell(3, 4).Range.Text = row.Specification;
            table.Cell(3, 6).Range.Text = row.FactoryNo;

            //在书签处插入文字
            object oStart = "client";//word中的书签名 
            Word.Range range = doc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 
            range.Text = row.Client;//在书签处插入文字内容

            //在书签处插入文字
            oStart = "explainReason";//word中的书签名 
            range = doc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 
            range.Text = row.ExplainReason;//在书签处插入文字内容

            //在书签处插入文字
            oStart = "noticeDate";//word中的书签名 
            range = doc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 
            if (!string.IsNullOrEmpty(row.NoticeDate))
            {
                string d = row.NoticeDate.Split(' ')[0];
                range.Text = d.Split('-')[0] + "年" + d.Split('-')[1] + "月" + d.Split('-')[2] + "日";
            }
            else {
                range.Text = "";
            }
           

            //在书签处插入文字
            oStart = "other";//word中的书签名 
            range = doc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 
            range.Text = row.Other;//在书签处插入文字内容

            object fontname = "Wingdings 2";
            object uic = true;
            //在书签处插入复选框
            oStart = "unableRepair";//word中的书签名 
            if (row.IsUnableRepair == 1)
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-4014, ref fontname, ref uic, ref missing);
            }
            else
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-3933, ref fontname, ref uic, ref missing);
            }

            //在书签处插入复选框
            oStart = "unableWork";//word中的书签名 
            if (row.IsUnableWork == 1)
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-4014, ref fontname, ref uic, ref missing);
            }
            else
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-3933, ref fontname, ref uic, ref missing);
            }

            //在书签处插入复选框
            oStart = "requireBack";//word中的书签名 
            if (row.IsRequireBack == 1)
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-4014, ref fontname, ref uic, ref missing);
            }
            else
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-3933, ref fontname, ref uic, ref missing);
            }

            //在书签处插入复选框
            oStart = "otherReason";//word中的书签名 
            if (row.IsOtherReason == 1)
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-4014, ref fontname, ref uic, ref missing);
            }
            else
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-3933, ref fontname, ref uic, ref missing);
            }

            //在书签处插入复选框
            oStart = "noticeClient";//word中的书签名 
            if (row.IsNoticeClient == 1)
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-4014, ref fontname, ref uic, ref missing);
            }
            else
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-3933, ref fontname, ref uic, ref missing);
            }

            //在书签处插入复选框
            oStart = "generationSend";//word中的书签名 
            if (row.IsGenerationSend == 1)
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-4014, ref fontname, ref uic, ref missing);
            }
            else
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-3933, ref fontname, ref uic, ref missing);
            }

            //在书签处插入复选框
            oStart = "back";//word中的书签名 
            if (row.IsBack == 1)
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-4014, ref fontname, ref uic, ref missing);
            }
            else
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-3933, ref fontname, ref uic, ref missing);
            }

            //在书签处插入复选框
            oStart = "isOther";//word中的书签名 
            if (row.IsOther == 1)
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-4014, ref fontname, ref uic, ref missing);
            }
            else
            {
                doc.Bookmarks.get_Item(ref oStart).Range.InsertSymbol(-3933, ref fontname, ref uic, ref missing);
            }

 			object LinkToFile = false;
            object SaveWithDocument = true;

            oStart = "applyUser";
            object Anchor = doc.Bookmarks.get_Item(ref oStart).Range;   //书签位置
            string pictureFileName = TempFileAccess.GetSignFile(row.ApplyUser);   //获得图片路径
            if (!string.IsNullOrEmpty(pictureFileName))
            {
                var InlineShape_name1 = doc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);    //插入图片
                //图片宽度
                InlineShape_name1.Width = 50f;
                //图片高度
                InlineShape_name1.Height = 25f;
            }
            else {
                doc.Bookmarks.get_Item(ref oStart).Range.Text = "";
            }
            


            //在书签处插入文字
            oStart = "applyTime";//word中的书签名 
            range = doc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 
            if (!string.IsNullOrEmpty(row.ApplyTime))
            {
                string d = row.ApplyTime.Split(' ')[0];
                range.Text = d.Split('-')[0] + "年" + d.Split('-')[1] + "月" + d.Split('-')[2] + "日";
            }
            else
            {
                range.Text = "";
            }

            oStart = "checkBackUser";
            Anchor = doc.Bookmarks.get_Item(ref oStart).Range;
            pictureFileName = TempFileAccess.GetSignFile(row.CheckBackUser);
            if (!string.IsNullOrEmpty(pictureFileName))
            {
                var InlineShape_name1 = doc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                //图片宽度
                InlineShape_name1.Width = 50f;
                //图片高度
                InlineShape_name1.Height = 25f;
            }
            else
            {
                doc.Bookmarks.get_Item(ref oStart).Range.Text = "";
            }


            //在书签处插入文字
            oStart = "checkBackTime";//word中的书签名 
            range = doc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 
            if (!string.IsNullOrEmpty(row.CheckBackTime))
            {
                string d = row.CheckBackTime.Split(' ')[0];
                range.Text = d.Split('-')[0] + "年" + d.Split('-')[1] + "月" + d.Split('-')[2] + "日";
            }
            else
            {
                range.Text = "";
            }

            oStart = "confirmUser";
            Anchor = doc.Bookmarks.get_Item(ref oStart).Range;
            pictureFileName = TempFileAccess.GetSignFile(row.ConfirmUser);
            if (!string.IsNullOrEmpty(pictureFileName))
            {
                var InlineShape_name1 = doc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                //图片宽度
                InlineShape_name1.Width = 50f;
                //图片高度
                InlineShape_name1.Height = 25f;
            }
            else
            {
                doc.Bookmarks.get_Item(ref oStart).Range.Text = "";
            }


            //在书签处插入文字
            oStart = "confirmTime";//word中的书签名 
            range = doc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 
            if (!string.IsNullOrEmpty(row.ConfirmTime))
            {
                string d = row.ConfirmTime.Split(' ')[0];
                range.Text = d.Split('-')[0] + "年" + d.Split('-')[1] + "月" + d.Split('-')[2] + "日";
            }
            else
            {
                range.Text = "";
            }

            object IsSave = true;
            doc.Close(ref IsSave, ref missing, ref missing);


            //打开系统文档预览
            try
            {
                var pdfPath = ReportControlEntry.DocToPdf(cachePath);
                System.Diagnostics.Process.Start(pdfPath);
            }
            catch (Exception e)
            {
                SystemService.MsgBoxService.ShowMsgBox(e.Message);
                return;
            }
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值