itextsharp使用ukey进行签章

2019/09/18 09:58
阅读数 184
public class UkeySign
    {
        //根据证书序列号查找证书
        private static X509Certificate2 GetCertificateFromStore(string serialNum)
        {
            try
            {
                //subjectName = "CN=" + subjectName;
                X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadWrite);
                X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
                foreach (X509Certificate2 x509 in storecollection)
                {
                    if (x509.SerialNumber==serialNum)
                    {
                        return x509;
                    }
                }
                store.Close();
                store = null;
                storecollection = null;
                return null;

            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// pdf签章
        /// </summary>
        /// <param name="sourceFile">源pdf</param>
        /// <param name="destFile">pdf保存路径</param>
        /// <param name="signImage">签章图片</param>
        /// <param name="certSerialNum">证书序列号</param>
        /// <param name="page">页码</param>
        /// <param name="lx"></param>
        /// <param name="ly"></param>
        /// <param name="rx"></param>
        /// <param name="ry"></param>
        /// <param name="signAll">是否所有页签章</param>
        /// <returns></returns>
        public static bool SignPdf(string sourceFile,string destFile,string signImage,string certSerialNum,int page,int lx,int ly,int rx,int ry,bool signAll=false,string signAuth="auth")
        {
            var cert = GetCertificateFromStore(certSerialNum);
            if(cert==null)
            {
                return false;
            }
            PdfReader pdfReader = null;
            FileStream signedPdf = null;
            PdfStamper pdfStamper = null;
            try
            {
                Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
                Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] {
            cp.ReadCertificate(cert.RawData)};

                IExternalSignature externalSignature =   new X509Certificate2Signature(cert, "SHA-1");
                var pathToBasePdf = sourceFile;

                pdfReader = new PdfReader(pathToBasePdf);

                 signedPdf = new FileStream(destFile, FileMode.Create);

                pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0',null,true);
                PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
                signatureAppearance.SignDate = DateTime.Now;
                signatureAppearance.SignatureCreator = signAuth;
                signatureAppearance.Reason = "I'm auth";
                var image = Image.GetInstance(signImage);
                signatureAppearance.SignatureGraphic = image;
                signatureAppearance.SignatureRenderingMode = RenderingMode.GRAPHIC;
                signatureAppearance.SetVisibleSignature(new Rectangle(lx, ly, rx, ry), page, "Signature1");

                MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
                return true;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (pdfReader != null)
                {
                    pdfReader.Close();
                }
                if (signedPdf != null)
                {
                    signedPdf.Close();
                }
                if (pdfStamper != null)
                {
                    
                    pdfStamper.Close();
                }
            }
            
        }
    }

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部