aspose利用word模板生成word、PDF

项目需求:表单归档时生成表单word、PDF文件

先上效果图

1、word模板(部分)书签 

2、生成结果图


开始上代码

 Dictionary<string, string> dictSource = new Dictionary<string, string>();
 dictSource = FlowCommon.FlowFun.NCRDocDic(FlowModel); //获取文字替换数据
 string docFIlePath = SetNCRDocImg(FlowModel, dictSource);//替换签名图片及文字内容,返回生成的word文件路径
 string path = Server.MapPath("~" + docFIlePath);
 FlowCommon.NPIOHelper.WordToPDF(path + "NCR.docx", path, "NCR");//根据word生成PDF
文字标签数据
public static Dictionary<string, string> NCRDocDic(Model.View_NCRFlowList FlowModel)
        {
            Dictionary<string, string> dictSource = new Dictionary<string, string>();
            try
            {
                dictSource.Add("zlgly_psjb", FlowModel.zlgly_psjb);//评审级别
                dictSource.Add("zlgly_bh", FlowModel.zlgly_bh);//编号
                dictSource.Add("jcy_mcth", FlowModel.jcy_mcth);//不合格品名称及图号
                dictSource.Add("jcy_fsdw", FlowModel.jcy_fsdw);//发生单位
                dictSource.Add("jcy_cpbh", FlowModel.jcy_cpbh);//产品编号
                dictSource.Add("jcy_fslb", FlowModel.jcy_fslb);//不合格品发生类别
                ...
            }
            catch (Exception e)
            {
                Log.Write(tabLog, e.ToString());
                dictSource = null;
            }
            return dictSource;
        }
/// <summary>
        /// NCR文档图片设置
        /// </summary>
        /// <param name="FlowModel"></param>
        /// <param name="dictSource"></param>
        /// <param name="IsGD"></param>
        public string SetNCRDocImg(Model.View_NCRFlowList FlowModel, Dictionary<string, string> dictSource, bool IsGD = true)
        {
            string docFIlePath = "";
            string templateFile = Server.MapPath("~/Templates/NCR.docx");
            Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);//读取模板

            DocumentBuilder builder = new DocumentBuilder(doc);
            //检查员
            string jcy = FlowModel.jcy_jcy;
            DataTable dt_jcy = FlowCommon.FlowFun.GetUserSignImg(jcy, "1"); //签名图片获取
            ReplaceImg(dt_jcy, "jcy_jcy", dictSource, builder);

            //质量管理员--判断等级
            string zlgly = FlowModel.zlgly_zlgly;
            DataTable dt_zlgly = FlowCommon.FlowFun.GetUserSignImg(zlgly, "2");
            ReplaceImg(dt_zlgly, "zlgly_zlgly", dictSource, builder);

            //使用文本方式替换
            foreach (string name in dictSource.Keys)
            {
                doc.Range.Replace(name, dictSource[name].Replace("\r\n", ""), true, true);
            }
            string SaveFile = "";
            if (IsGD)  //默认归档操作,归档与打印文件保存路径不同
                docFIlePath = "/upload/NCRFiles/" + FlowModel.zlgly_bh + "/";
            else
                docFIlePath = "/upload/PrintFiles/" + DateTime.Now.Ticks.ToString() + "/";
            SaveFile = Server.MapPath("~" + docFIlePath);
            if (!Directory.Exists(SaveFile))
            {
                Directory.CreateDirectory(SaveFile);
            }

            doc.Save(SaveFile + "NCR.docx");
            return docFIlePath;
        }
 /// <summary>
        /// 签名图片替换
        /// </summary>
        /// <param name="dt">数据源</param>
        /// <param name="name">标签名称</param>
        /// <param name="dictSource"></param>
        /// <param name="builder"></param>
        /// <param name="IsMul">是否为多图片</param>
        private void ReplaceImg(DataTable dt, string name, Dictionary<string, string> dictSource, DocumentBuilder builder, bool IsMul = false)
        {
            if (dt != null && dt.Rows.Count > 0)  //查到数据了
            {
                if (!IsMul)
                {
                    string url = dt.Rows[0]["signurl"].ToString();
                    //PHOTO_jcy
                    string imgurl = Server.MapPath("~" + url);
                    FileInfo fileInfo = new FileInfo(imgurl);
                    if (fileInfo.Exists)
                    {
                        builder.MoveToBookmark("PHOTO_" + name);
                        builder.InsertImage(imgurl, RelativeHorizontalPosition.Default, -5, RelativeVerticalPosition.Margin, 0, 45, 18, WrapType.None);
                        dictSource[name] = ""; //签名图片存在时清空文字标签内容
                    }
                }
                else //客户需求有多人签名,在同一个表单框中,最多现在三个签名,这里根据自己的业务需求来
                {
                    int i = 1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (i > 3) break;
                        string url = dr["signurl"].ToString();
                        //PHOTO_jcy
                        string imgurl = Server.MapPath("~" + url);
                        FileInfo fileInfo = new FileInfo(imgurl);
                        if (fileInfo.Exists)
                        {
                            builder.MoveToBookmark("PHOTO_" + name + i.ToString());
                            builder.InsertImage(imgurl, RelativeHorizontalPosition.Default, -5, RelativeVerticalPosition.Margin, 0, 45, 18, WrapType.Square);
                            dictSource[name] = ""; //
                        }
                        i++;
                    }
                }
            }
        }
/// <summary>
        /// Word转成Pdf
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成Pdf的保存路径</param>
 /// <param name="wordFileName">转换成html的文件名字</param> 
public static void WordToPDF(string path, string savePath, string wordFileName)
        {
            Aspose.Words.Document d = new Aspose.Words.Document(path);
            d.Save(savePath + wordFileName + ".pdf", SaveFormat.Pdf);
        }

 



aspose.word、aspose.pdf dll 下载地址
http://download.csdn.net/download/tt871911/10229509

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值