2016-06-15 94 views
0

我正在嘗試對任何給定pdf的每個頁面進行數字簽名。但它只在第一頁或最後一頁簽字。我想我已經知道問題出在MakeSignature.SignDetached()方法。此方法關閉所有流並關閉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的庫。有沒有解決這個問題的方法?

+0

嚴格地說,「[如何使用iText在所有文檔的頁面中顯示數字PDF簽名](http://stackoverflow.com/a/35724742/1729265)」是關於iText,而不是iTextSharp,但來自那裏的參數保持一致。 – mkl

+0

謝謝@mkl,我不知道同比對所有論點做了概述。我剛剛提出了它。 –

+0

@furiousNoob *此方法關閉所有流並關閉pdf以進行進一步簽名。* - 如果您已經意識到集成PDF簽名的問題,請選擇「多個簽名(每頁一個),每個單獨一個可視化文件」我引用的答案,現在有實施困難,請相應更新您的問題。它最終會重新打開並回答。 – mkl

回答

0

沒有「在PDF中標記每一頁」之類的東西。數字簽名(可見或不可見)在整個文檔上簽名。

「簽名頁面」的概念在PDF中不存在。

如果您使用可見簽名,則可以在PDF中的頁面上放置小部件註釋。一個簽名只能對應一個頁面上的一個窗口小部件註釋。

閱讀ISO-32000-1時可能並不明確,但在ISO-32000-2中已明確說明。

總之:沒有答案,因爲你的問題是錯的。您使用該簽名的小部件註釋混淆了數字簽名(可能不可見,簽署完整文檔:所有頁面,所有附件,所有元數據)。

PS:此消息由位於柏林的the PDF Days生效。在一個半小時內(在柏林11:45),您可以關注iText工程師爲您提供的這個主題的直播。請參見https://twitter.com/iText/status/742975159976493056

+0

*您可以關注直播視頻* - 哎呀,我不知道直播視頻。只是在這裏打開它,thanx。 ;) – mkl

+0

我在昨天的演講中提到過你:在SO(3枚金牌)上有3位PDF大師。其中2個是在柏林的PDF日。我們想念你... –

相關問題