• 签章


    /// <summary>
            /// 盖章
            /// </summary>
            /// <param name="contract"></param>
            /// <param name="buyer"></param>
            /// <param name="seller"></param>
            /// <returns></returns>
            private ReturnJsonCode Sign(tbl_contract contract, string buyer, string seller)
            {
                ReturnJsonCode jsonCode = new ReturnJsonCode();
                string path = "/files/word/" + contract.ID / 1000 + "/" + contract._id + contract.extension;
                string localPath = System.Web.HttpContext.Current.Server.MapPath(path);
                using (FileStream fs = File.Open(localPath, FileMode.Open))
                {
                    jsonCode = SaveFileKyb(fs, contract, seller, buyer);
                }
                return jsonCode;
            }
    
    /// <summary>
            /// 保存文件到
            /// </summary>
            /// <returns>pdf文件的base64字符串</returns>
            public ReturnJsonCode SaveFileKyb(Stream InputStream, tbl_contract newContract, string seller, string buyer)
            {
                ReturnJsonCode jsonCode = new ReturnJsonCode();
                try
                {
                    string url = "http://" + ConfigurationManager.AppSettings[""] + "/api/contract/Default.aspx";
    
                    byte[] data = new byte[InputStream.Length];
                    InputStream.Position = 0;//重新定位
                    InputStream.Read(data, 0, data.Length);
                    string base64Str = Convert.ToBase64String(data);
                    Dictionary<string, object> dicParam = new Dictionary<string, object>();
                    dicParam.Add("Action", "CreatePdf");
                    dicParam.Add("SiteId", yunId);
                    dicParam.Add("ContractId", newContract.ID);
                    dicParam.Add("Data", System.Web.HttpContext.Current.Server.UrlEncode(base64Str));
                    dicParam.Add("BuyerKyh", buyer);
                    dicParam.Add("SellerKyh", seller);
                    //订单不为空,添加订单信息
                    if (newContract.orders != null && newContract.orders.Count > 0)
                    {
                        dicParam.Add("Orders", System.Web.HttpContext.Current.Server.UrlEncode(newContract.orders.ObjectToString()));
                    }
                    //添加权限参数
                    common.CreateApiAccount(dicParam);
                    string strResult = HttpMethod.WebRequestPost(url, dicParam);
                    if (string.IsNullOrWhiteSpace(strResult))
                    {
                        jsonCode.code = -1;
                        jsonCode.msg = "请求结果为空";
                    }
                    else
                    {
                        jsonCode = Serialize.DeSerialize<ReturnJsonCode>(strResult);
                    }
                }
                catch (Exception ex)
                {
                    jsonCode.code = -1;
                    jsonCode.msg = "保存pdf到科易宝出错:" + ex.Message;
                }
                if (jsonCode.code == 1)
                {
                    ReturnJsonCode jc = SavePdfToLocal(jsonCode.data, newContract);
                    if (jc.code == -1)
                    {
                        return jc;
                    }
                }
                return jsonCode;
            }
    
    
    /// <summary>
            /// 保存pdf到本地
            /// </summary>
            /// <param name="base64Str"></param>
            /// <param name="contract"></param>
            public ReturnJsonCode SavePdfToLocal(string base64Str, tbl_contract contract)
            {
                ReturnJsonCode jsonCode = new ReturnJsonCode();
                string path = System.Web.HttpContext.Current.Server.MapPath("/files/contract/" + (contract.ID / 1000) + "/" + contract._id.ToString() + ".pdf");
                if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("/files")))
                {
                    Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("/files"));
                }
                if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("/files/contract")))
                {
                    Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("/files/contract"));
                }
                if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("/files/contract/" + (contract.ID / 1000))))
                {
                    Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("/files/contract/" + (contract.ID / 1000)));
                }
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                FileStream fs = null;
                try
                {
                    byte[] d = Convert.FromBase64String(base64Str);
                    fs = File.Create(path);
                    fs.Write(d, 0, d.Length);
                    jsonCode.code = 1;
                    jsonCode.msg = "保存成功";
                }
                catch (Exception ex)
                {
                    jsonCode.code = -1;
                    jsonCode.msg = "保存pdf到本地出错:" + ex.Message;
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.Dispose();
                    }
                }
                return jsonCode;
            }
    

      

    /// <summary>
        /// 根据Word文件Base64字符串创建PDF
        /// </summary>
        /// <param name="p_jsonCode"></param>
        private void CreatePdf(ReturnJsonCode p_jsonCode)
        {
            int iCid = 0;
            int.TryParse(Base.GetPost("ContractId"), out iCid);//合同ID
            int iSiteId = 0;
            int.TryParse(Base.GetPost("SiteId"), out iSiteId);//平台ID
            string strWordData = Base.GetPost("Data");//word文件Base64字符串
            string strBuyerKyh = Base.GetPost("BuyerKyh");//买方号
            string strSellerKyh = Base.GetPost("SellerKyh");//卖方号
            string strOrderData = Base.GetPost("Orders");//订单信息
            if (iCid <= 0)
            {
                p_jsonCode.msg = "cid参数错误";
                return;
            }
            int iBuyerUid = 0;//买方科易宝用户ID
            int iSellerUid = 0;//卖方科易宝用户ID
            if (!string.IsNullOrWhiteSpace(strBuyerKyh))
            {
                iBuyerUid = Base.GetUidByK8008username(strBuyerKyh);
            }
            if (!string.IsNullOrWhiteSpace(strSellerKyh))
            {
                iSellerUid = Base.GetUidByK8008username(strSellerKyh);
            }
            List<fjcctt_order> listOrders = null;
            if (!string.IsNullOrWhiteSpace(strOrderData))
            {
                listOrders = JsonConvert.DeserializeObject<List<fjcctt_order>>(strOrderData);
            }
            Base.WriteLog("订单参数为:" + strOrderData);
            fjcctt.contract objContract = new fjcctt.contract();
            objContract.ContId = iCid;
            objContract.SiteId = iSiteId;
            objContract.BuyerId = iBuyerUid;
            objContract.SellerId = iSellerUid;
            objContract.Orders = listOrders;
            objContract.CreatePdf(strWordData);
            if (objContract.Success)
            {
                p_jsonCode.code = 1;
                p_jsonCode.data = objContract.Msg;
            }
            else
            {
                p_jsonCode.msg = objContract.Msg;
            }
        }
    
     /// <summary>
            /// 将WORD文件流转为PDF字符串
            /// </summary>
            /// <param name="p_stream"></param>
            /// <returns></returns>
            public void CreatePdf(string p_strword)
            {
                if (ContId <= 0)
                {
                    _success = false;
                    _msg = "参数错误";
                    return;
                }
                TxtLog objlog = new TxtLog();
                objlog.dir = HttpContext.Current.Server.MapPath(strLogPath);
                Stream smPdf = null;
                Stream smWord = null;
                try
                {
                    string strDirPath = HttpContext.Current.Server.MapPath(string.Format("{0}temp/externalsite/{1}/{2}/", ContractRootPath, SiteId, ContId / 1000));
                    if (!Directory.Exists(strDirPath))
                    {
                        Directory.CreateDirectory(strDirPath);
                    }
                    string strWordPath_Temp = string.Format("{0}{1}_temp.doc", strDirPath, ContId);//WORD文件地址
                    string strNoImgPdfPath_Temp = string.Empty;//不带签章PDF地址
                    string strPdfPath_Temp = string.Empty;//带签章PDF地址
                    Document objDoc = null;
                    //base64不为空,从base64读取word
                    if (!string.IsNullOrWhiteSpace(p_strword))
                    {
                        byte[] dataWord = Convert.FromBase64String(p_strword);
                        smWord = new MemoryStream(dataWord);
                        objDoc = new Document(smWord);
                        objDoc.Save(strWordPath_Temp);
                    }//从word读取
                    else if (File.Exists(strWordPath_Temp))
                    {
                        objDoc = new Document(strWordPath_Temp);
                    }
                    else
                    {
                        _success = false;
                        _msg = "word文件不存在";
                        objlog.WriteLog(string.Format("word文件不存在,参数:{0}", p_strword));
                        return;
                    }
    
                    //生成不带签章的pdf文件
                    strNoImgPdfPath_Temp = string.Format("{0}{1}_noimg_temp.pdf", strDirPath, ContId);
                    //添加订单信息
                    if (Orders != null && Orders.Count > 0)
                    {
                        InsertTable(objDoc);
                    }
                    objDoc.Save(strNoImgPdfPath_Temp);
                    if (File.Exists(strNoImgPdfPath_Temp))
                    {
                        strPdfPath_Temp = string.Format("{0}{1}_temp.pdf", strDirPath, ContId);
                        //加载签章
                        string strBuyerSeal = string.Empty;
                        if (BuyerId > 0)
                        {
                            strBuyerSeal = HttpContext.Current.Server.MapPath(string.Format("{0}{1}/{2}.png", SealRootPath, BuyerId / 1000, BuyerId));
                        }
                        string strSellerSeal = string.Empty;
                        if (SellerId > 0)
                        {
                            strSellerSeal = HttpContext.Current.Server.MapPath(string.Format("{0}{1}/{2}.png", SealRootPath, SellerId / 1000, SellerId));
                        }
                        if (BuyerId > 0 || SellerId > 0)
                        {
                            InsertWatermark(strNoImgPdfPath_Temp, strPdfPath_Temp, strBuyerSeal, strSellerSeal);
                            //签章失败,则终止操作
                            if (!_success)
                            {
                                return;
                            }
                            //已签章,获取已签章PDF文件
                            smPdf = File.OpenRead(strPdfPath_Temp);
                        }
                        else
                        {
                            //未签章,获取未签章PDF文件
                            smPdf = File.OpenRead(strNoImgPdfPath_Temp);
                        }
                        byte[] data = new byte[smPdf.Length];
                        smPdf.Read(data, 0, data.Length);
                        _msg = Convert.ToBase64String(data);
                        _success = true;
                    }
                    else
                    {
                        _success = false;
                        _msg = "文件存储失败";
                        objlog.WriteLog(string.Format("文件存储失败,参数:{0}", p_strword));
                    }
                }
                catch (Exception ex)
                {
                    _success = false;
                    _msg = ex.Message;
                }
                finally
                {
                    if (smPdf != null)
                    {
                        smPdf.Dispose();
                    }
                    if (smWord != null)
                    {
                        smWord.Dispose();
                    }
                }
            }
    

      

  • 相关阅读:
    《java入门第一季》之面向对象(继承总结)
    《java入门第一季》之面向对象面试题
    《java入门第一季》之面向对象(this和super详细分析)
    《java入门第一季》之面向对象(方法重写问题)
    《java入门第一季》之面向对象面试题(继承中构造方法的关系)
    《java入门第一季》之面向对象面试题(this和super的区别)
    《java入门第一季》之面向对象(继承)
    《java入门第一季》之面向对象(继承)
    Express服务器开发
    WebView 缓存原理分析和应用
  • 原文地址:https://www.cnblogs.com/sophiel/p/9056102.html
Copyright © 2020-2023  润新知