使用iTextSharp签署PDF表格

时间:2011-04-13 07:40:39

标签: itextsharp digital-signature

我有一个PDF表格,其中有一个已知名称为root [0]的签名字段.containerpage [0] .footer [0] .SignatureField1 [0]
我也有PFX证书 现在问题是,我在itextsharp的PdfStamper中打开了表单,如何签名以便

  • 表单已锁定为更改
  • 签名状态在上述字段中可见

我提出了以下代码:

Dim pk12 As Pkcs12Store = New Pkcs12Store(New FileStream(Application.StartupPath & "cert.pfx", FileMode.Open, FileAccess.Read), "pass".ToCharArray())
Dim i As IEnumerator = pk12.Aliases()
While (i.MoveNext())
    cert_alias = (i.Current)
    If (pk12.IsKeyEntry(cert_alias)) Then
        Exit Sub
    End If
End While
Dim akp As AsymmetricKeyParameter = pk12.GetKey(cert_alias).Key
Dim ce As X509CertificateEntry() = pk12.GetCertificateChain(cert_alias)
Dim chain() As X509Certificate( **%PROBLEM%** )
For k As Integer = 0 To ce.Length
    chain(k) = ce(k).getCertificate()
Next
Dim reader As PdfReader = New PdfReader("pdf.pdf")
Dim stamper As PdfStamper = PdfStamper.CreateSignature(reader, New FileStream("pdf.pdf", "pdf_signed.pdf"), FileMode.Create, FileAccess.Write), "\0")

Dim appearance As PdfSignatureAppearance = stamper.SignatureAppearance
appearance.SetVisibleSignature("mySig")
appearance.Reason = "Its personal."
appearance.Location = "Foobar"
appearance.SetCrypto(pk12, chain, Nothing, PdfSignatureAppearance.WINCER_SIGNED)
appearance.CertificationLevel = PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED

appearance.Acro6Layers = True
appearance.SignatureGraphic = Image.GetInstance(RESOURCE)
appearance.Render = PdfSignatureAppearance.SignatureRender.Graphic


stamper.Close()
然而,

无法弄清楚 X509CertificateStructure 应该在X509Certificate中作为代码块中的%PROBLEM%的参数。

谢谢

1 个答案:

答案 0 :(得分:0)

我认为应该是:

Dim chain() As X509Certificate(ce.length)

我总是对VB和数组大小声明感到困惑,所以你可能不得不减去1