Пример #1
0
        private bool DoSignPdfFile(Secure pdf, ConversionProfile profile, X509Certificate2 certificate, TimeServerAccount timeServerAccount)
        {
            var signatureSettings = profile.PdfSettings.Signature;

            if (!certificate.HasPrivateKey)
            {
                _logger.Error("Canceled signing. The certificate '" + signatureSettings.CertificateFile + "' has no private key.");
                throw new ProcessingException(
                          "Canceled signing. The certificate '" + signatureSettings.CertificateFile + "' has no private key.", ErrorCode.Signature_NoPrivateKey);
            }

            using (var signature = new Pdftools.PdfSecure.Signature())
            {
                signature.Provider             = ""; //"Microsoft Base Cryptographic Provider v1.0;123456";
                signature.Name                 = certificate.GetNameInfo(X509NameType.SimpleName, false);
                signature.Store                = "MY";
                signature.StoreLocation        = 1; // 0 = Local Machine; 1 = Current User
                signature.SignerFingerprintStr = certificate.Thumbprint;

                signature.ContactInfo = signatureSettings.SignContact;
                signature.Location    = signatureSettings.SignLocation;
                signature.Reason      = signatureSettings.SignReason;

                signature.PageNo = GetSignaturePageNumber(signatureSettings);
                signature.Rect   = GetSignatureRect(signatureSettings);

                var timeServerUri = new UriBuilder(timeServerAccount.Url);

                if (timeServerAccount.IsSecured)
                {
                    timeServerUri.UserName = Uri.EscapeDataString(timeServerAccount.UserName);
                    timeServerUri.Password = Uri.EscapeDataString(timeServerAccount.Password);
                }

                signature.TimeStampURL = timeServerUri.ToString();

                signature.FillColor   = 16777215; //White
                signature.StrokeColor = 13158600; //Grey

                if (!signatureSettings.AllowMultiSigning &&
                    profile.OutputFormat != OutputFormat.PdfA1B)
                {
                    return(pdf.AddDocMDPSignature(signature, 1));
                }

                return(pdf.AddSignature(signature));
            }
        }