如何在没有MakeSignature.SignDetached()方法的情况下对pdf进行数字签名?

时间:2016-06-15 05:27:58

标签: c# pdf itextsharp

我正在尝试对任何给定pdf的每个页面进行数字签名。但它只在第一页或最后一页签名。我想我已经发现问题出在 MakeSignature.SignDetached()方法中。此方法关闭所有流并关闭pdf以进一步签名。

我的代码:

 public static void SignInForEveryPage(string input, string output, PDFEncryption pdfEnc, bool encrypt, bool passCheck, string pass) {
            X509CertificateParser cp = new X509CertificateParser();
            X509Certificate[] chain = { cp.ReadCertificate(CertInfo.MyCert.RawData) };

            IExternalSignature externalSignature = new X509Certificate2Signature(CertInfo.MyCert, "SHA-1");
            //Setup signature
            if(File.Exists(output)) {
                File.Delete(output);
            }
            PdfSignatureAppearance signatureAppearance=null;
            PdfSignatureAppearance tempAppearance = null;

            PdfReader reader = new PdfReader(input);
            FileStream firstFileStream = new FileStream(output, FileMode.Create, FileAccess.ReadWrite);
            PdfStamper pdfStamper = PdfStamper.CreateSignature(reader, firstFileStream, '\0', null, true);

            for(int index = 1; index <= reader.NumberOfPages; index++) {
                if(encrypt && pdfEnc != null) {
                    pdfEnc.Encrypt(pdfStamper);
                }
                if(passCheck) {
                    pdfStamper.SetEncryption(PdfWriter.STRENGTH128BITS, "123", "123", PdfWriter.ALLOW_COPY);
                    //Set password of output file
                }

                //Write the metadata
                pdfStamper.MoreInfo = MetaData.GetMetaData();
                pdfStamper.XmpMetadata = MetaData.GetStreamedMetaData();

                //Set signature appearance
                 signatureAppearance = pdfStamper.SignatureAppearance;
                signatureAppearance.Reason = ReasonText; //Reason
                signatureAppearance.Contact = ContactText; //Contact
                signatureAppearance.Location = LocationText; //Location

                byte[] rawData = null;
                var customText = "";

                //Set the text shown in signature
                customText += "Digitally Signed by:\n";
                customText += CertInfo.CertName + "\n";

                if(!string.IsNullOrEmpty(LocationText)) {
                    customText += "Location: ";
                    customText += LocationText + "\n";
                }

                if(!string.IsNullOrEmpty(ReasonText)) {
                    customText += "Reason: ";
                    customText += ReasonText + "\n";
                }

                customText += "Date: ";
                customText += DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss K") + "\n";
                customText = customText.TrimEnd();

                //set the image shown in signature
                if(ShowImage && SignaturePictureImage != null) {
                    using(MemoryStream memoryStream = new MemoryStream()) {
                        SignaturePictureImage.Save(memoryStream, ImageFormat.Bmp);
                        rawData = memoryStream.ToArray();
                    }
                }

                //For signature position and size
                var sigX = Mm2Pt(LeftNumValue);
                var sigY = Mm2Pt(BottomNumValue);
                var sigW = Mm2Pt(WidthNumValue);
                var sigH = Mm2Pt(HeightNumValue);

                //Draw the rectangle for signature field
                //pdfStamper.Reader.GetPageSize(index);
                signatureAppearance.SignatureGraphic = rawData == null ? null : iTextSharp.text.Image.GetInstance(rawData);
                signatureAppearance.Layer2Text = customText;
                signatureAppearance.Layer4Text = ""; //if null or not set then it will show 'signature not valid'
                signatureAppearance.Acro6Layers = true;
                if(signatureAppearance.SignatureGraphic != null) {
                    signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
                    //show image first then text in the signature
                }
                signatureAppearance.SetVisibleSignature(new Rectangle(sigX, sigY, sigX + sigW, sigY + sigH), index, null);
                signatureAppearance.GetLayer(1);
                tempAppearance = signatureAppearance;
                MakeSignature.SignDetached(tempAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
            }
        }

我正在使用iTextSharp库。有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

没有&#34;对PDF中的每个页面进行签名。&#34;数字签名(可见或不可见)在整个文档上签名。

&#34;签署页面的概念&#34;根本不存在于PDF中。

如果使用可见签名,则会在PDF的页面上放置窗口小部件注释。一个签名只能与一个页面上的一个窗口小部件注释相对应。

在阅读ISO-32000-1时可能不太清楚,但在ISO-32000-2中明确说明了这一点。

简而言之:您的问题没有答案,因为您的问题是错误的。您将数字签名(可能不可见,签署完整文档:所有页面,所有附件,所有元数据)与该签名的窗口小部件注释混淆。

PS:此消息是从柏林的the PDF Days开始的。在1个半小时(柏林11:45),您可以关注iText工程师带给您的这个主题的直播。见https://twitter.com/iText/status/742975159976493056