2013-09-25 36 views
0

當我使用ITextSharp對已使用此代碼進行數字簽名的文檔進行數字簽名時,會使其他簽名無效。如果我使用文字或圖片進行數字簽名,它可以正常工作。這是Adobe/Itextsharp限制還是代碼有問題?ITextsharp在使用線條或矩形時使簽名無效

public void SignWithLine(string pdfFilePath, string outputFilePath, LineAnnotation lineAnnotation) 
{   
    double xStartPoint = 89.285969238281268, yStartPoint = 343.08978515624881, xEndPoint = 72.7515234375, yEndPoint = 496.03341796874878, lineStroke = .24; 

    CertificateWrapper certificate = CertificateWrapper.GetCertificateInformationFromSignature(GetCertificateInformation()); 
    PdfReader reader = new PdfReader(pdfFilePath); 
    PdfTemplate layer = null; 
    using (PdfStamper signature = PdfStamper.CreateSignature(reader, null, '\0', outputFilePath, true)) 
    { 
     PdfSignatureAppearance signatureAppearance = signature.SignatureAppearance; 
     signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC; 
     Rectangle rect = new Rectangle((float)Math.Min(xStartPoint, xEndPoint), (float)Math.Min(yStartPoint, yEndPoint), (float)Math.Min(xStartPoint, xEndPoint) + (float)Math.Abs(xEndPoint - xStartPoint), (float)Math.Min(yStartPoint, yEndPoint) + (float)Math.Abs(yEndPoint - yStartPoint)); 
     signatureAppearance.SetVisibleSignature(rect, lineAnnotation.PageIndex + 1, GetCertificateFieldName()); 
     layer = signatureAppearance.GetLayer(2); 

     PdfContentByte cb = signature.GetUnderContent(lineAnnotation.PageIndex + 1); 
     cb.SetLineWidth((float)lineStroke); 
     cb.MoveTo((float)xStartPoint, (float)yStartPoint); 
     cb.LineTo((float)(xEndPoint), (float)(yEndPoint)); 
     cb.Stroke(); 

     signatureAppearance.CertificationLevel = PdfSignatureAppearance.NOT_CERTIFIED; 
     // Normal signature, not a certification 
     MakeSignature.SignDetached(signatureAppearance, certificate.DigitalSignature, certificate.Chain, null, null, null, 0, true); 

     signature.Close(); 
    } 
} 
+0

的Adobe保護的簽名。因此,如果您嘗試退出文檔,原始簽名將被刪除。這是一項安全功能。 – FeliceM

回答

1

signature是您的PdfStamper。您在

上畫一條線
PdfContentByte cb = signature.GetUnderContent(lineAnnotation.PageIndex + 1); 

即,您將其繪製在頁面的內容流中。這被視爲頁面內容的改變,因此被原始簽名禁止。有關允許更改的詳細信息,請參閱this answer

0

我找到了一種通過修改iTextSharp庫來實現這個功能的方法。

//首先我重載SetVisibleSignature通過INT

公共無效SetVisibleSignature的列表(矩形pageRect,字符串fieldName的,列表分頁) { 頁=分頁; //頁面是私人列表頁面; ... }

//其次,在PreClose事件中,你更新如下圖所示

  sigField.Page = pagen; 
      if (pagen != 0) 
       writer.AddAnnotation(sigField, pagen); 
      else if (pages != null && pages.Count > 0) 
       pages.ForEach(f => writer.AddAnnotation(sigField, f)); // this annotates all pages with the same signature 
      else 
       throw new DocumentException("No pages specified for signature."); 
+0

*我找到了一種方法來做到這一點* - 您的答案是關於**在多個頁面上顯示您的簽名**,但問題是**在使用線條或矩形**時使簽名無效。也許你想回答一個不同的問題? ......這就是說,PDF規範說「給定的註釋字典應該僅從一個頁面的Annots數組中引用」([ISO 32000-1](http://www.adobe.com的第12.5.2節) /content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf))。因此,PDF查看器可能會拒絕您的PDF文件被破壞。如果他們現在不這樣做,未來的版本可能會做。 – mkl

+1

是的,我很抱歉。我以爲我正在回答一個不同的問題。 – hawk

相關問題