public void Signature(string filePath, string savePath, string keyPath, string postext) { //throw new NotImplementedException(); KGPdfHummer hummer = null; FileStream os = null; try { int index = filePath.LastIndexOf('\\'); string fileName = filePath.Substring(index + 1); //原始文档路径 os = new FileStream(savePath, FileMode.Create, FileAccess.ReadWrite); //签章或者数字签名后输出文件路径 AbstractSign pdfExecute = null; /*进行电子签章*/ hummer = KGPdfHummer.CreateInstance(filePath, null, true, os, true); //创建文档编辑实例。 pdfExecute = new PdfElectronicSeal4KG(keyPath, 1, "123456"); //与.key文件对接,签章编号默认为1,密码默认123456 pdfExecute.SetPagen(hummer.GetNumberOfPages()); //只在pdf最后一页加载电子签章 hummer.AddExecute(pdfExecute); pdfExecute.SetText(postext); //根据文字定位 hummer.DoExecute(); //执行电子签章任务 File.Delete(filePath); } catch (Exception e) { e.ToString(); } finally { if (os != null) { try { os.Close(); } catch { } } if (hummer != null) { hummer.Close(); } } }
public void ProcessRequest(HttpContext context) { string option = context.Request.Form["option"]; if (option == null) { option = context.Request.QueryString["option"]; } if ("calculateHash".Equals(option)) { string cert = context.Request.Form["cert"]; string imgData = context.Request.Form["imgData"]; string imgWidth = context.Request.Form["imgWidth"]; string imgHeight = context.Request.Form["imgHeight"]; string imgExt = context.Request.Form["imgExt"]; string fileIDS = context.Request.Form["fileIDS"]; string xPoint = context.Request.Form["xPoint"]; string yPoint = context.Request.Form["yPoint"]; string signatureDtflag = ConvertEx.ToString(context.Request.Form["signatureDtflag"]); bool signatureAllPagePdfflag = ConvertEx.ToBool(context.Request.Form["signatureAllPagePdfflag"]); //是否签所有页 string signaturePagen = context.Server.UrlDecode(context.Request.Form["signaturePagen"]); //特定页码集合 Hashtable pdfList = GetCleanPdfPath(fileIDS); if (pdfList.Count > 0) { KGPdfHummer hummer = null; string tagPdf = string.Empty; FileStream files = null; try { if (imgData == null) { throw new Exception("签章获取错误!"); } else { JArray objList = new JArray(); X509CertificateParser x509CertificateParser; X509Certificate x509Certificate; DigitalSignatureByKey digitalSignatureByKey; PdfSignature pdfSignature; PdfSignature4KG pdfSignature4KG = new PdfSignature4KG(null, 1, null); KGBase64 base64; KGTextInfo kgTextInfo; string base64str, data; int width, height; JObject obj; byte[] imgb; foreach (DictionaryEntry de in pdfList) { string sourcePdf = de.Value.ToString(); tagPdf = GetTagertPdf(sourcePdf); files = new FileStream(tagPdf, FileMode.Create); hummer = KGPdfHummer.CreateSignature(sourcePdf, null, true, files, null, true); x509CertificateParser = new X509CertificateParser(); // 公钥证书 x509Certificate = x509CertificateParser.ReadCertificate(Hex.Decode(cert)); digitalSignatureByKey = new DigitalSignatureByKey(); hummer.SetCertificate(x509Certificate, digitalSignatureByKey); pdfSignature = hummer.PdfSignature; base64 = new KGBase64(); base64str = imgData.Substring(0, 65); data = imgData.Substring(65); base64.SetBase64Table(base64str); imgb = base64.Decode(data); width = (int)(float.Parse(imgWidth) * 7200F / 254F); height = (int)(float.Parse(imgHeight) * 7200F / 254F); pdfSignature.SetImage(imgb, imgExt.Substring(1), width, height); ArrayList pagenList = new ArrayList(); string[] spPagen = (signaturePagen + ",").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var vr in spPagen) { pagenList.Add(ConvertEx.ToInt(vr)); } if (pagenList.Count < 1) { pagenList.Add(1); } if (!signatureAllPagePdfflag) //不签所有页,则签特定页 { pdfSignature.SetPagen((Int32[])pagenList.ToArray(typeof(Int32))); } //if (searTxt != "") //{ // pdfSignature.SetText(searTxt, true); //} //else //{ // pdfSignature.SetXY(200F, 200F); //} pdfSignature.SetXY(float.Parse(xPoint), float.Parse(yPoint)); hummer.PdfSignature = pdfSignature; if (signatureDtflag == "1") //签章加日期 { pdfSignature4KG.SetImage(imgb, imgExt.Substring(1), width, height); if (!signatureAllPagePdfflag)//不签所有页,则签特定页 { pdfSignature4KG.SetPagen((Int32[])pagenList.ToArray(typeof(Int32))); } pdfSignature4KG.SetXY(float.Parse(xPoint), float.Parse(yPoint)); kgTextInfo = new KGTextInfo(DateTime.Now.ToString("yyyy年MM月dd日")); kgTextInfo.Font = new System.Drawing.Font("宋体", 12); kgTextInfo.PosType = TextInfoPosType.OUT_SOUTH_MIDDLE; pdfSignature4KG.AddImgText(kgTextInfo); hummer.PdfSignature = pdfSignature4KG; } hummer.DoSignature(); obj = new JObject() { new JProperty("fileID", de.Key.ToString()), new JProperty("file", tagPdf), new JProperty("hash", digitalSignatureByKey.GetHash()), new JProperty("contentsPostion", digitalSignatureByKey.GetContentsPostion()), }; objList.Add(obj); } context.Response.Write(objList.ToString()); } } catch (Exception ex) { if (ex.Message.Contains("iSignature_PDF_API中间件已过期")) { context.Response.Write("系统签章过期"); } else { context.Response.Write("系统签章异常:" + ex.Message.ToString()); } } finally { try { if (files != null) { files.Close(); } } catch { } if (hummer != null) { hummer.Close(); } } } } else if ("rewriteSigned".Equals(option)) { string cert = context.Request.Form["cert"]; JArray objList = (JArray)JsonConvert.DeserializeObject(context.Request.Form["jArray"]); if (objList != null && objList.Count > 0) { // 公钥证书 X509CertificateParser x509CertificateParser = new X509CertificateParser(); X509Certificate x509Certificate = x509CertificateParser.ReadCertificate(Hex.Decode(cert)); DigitalSignatureByKey digitalSignatureByKey; foreach (JObject obj in objList) { string fileID = Common.ConvertEx.ToString(obj["fileID"]); string pdfPath = Common.ConvertEx.ToString(obj["file"]); long pos = Common.ConvertEx.ToLong(obj["contentsPostion"]); string contents = Common.ConvertEx.ToString(obj["contents"]); digitalSignatureByKey = new DigitalSignatureByKey(); digitalSignatureByKey.RewriteContents(pdfPath, x509Certificate.GetEncoded(), pos, contents); UpdateSignatureStatus(fileID, pdfPath, context); } } context.Response.Write("完成域数字签名!"); } }