The DigitalSignature sample for multiple digital signatures is (no longer) working.
My application has code that used to work (with 16.1.0.37?), but now there is a problem when I try to add a second signature.
Attached you can find the pdf generated with your sample webapp called DigitalSignature. The signatures are not valid.
I upgraded the nuget packages to the latest version and the problem still persists.
I hope you can help.
Function Sign(signType As SignType, strPDFFile As String, img As Image, strCertificate As String) As Boolean
If signType = SignType.None Then
'* skip
Return True
Else
Try
Dim signatureImage As PdfBitmap = New PdfBitmap(img)
Dim docStream As FileStream = New FileStream(strPDFFile, FileMode.Open, FileAccess.Read)
Dim memstream As MemoryStream = New MemoryStream()
Dim LoadedDocument As New PdfLoadedDocument(docStream)
If IsNothing(LoadedDocument.Form) Then
LoadedDocument.CreateForm()
End If
Dim intIndex As Integer = 0
Dim matchRect As New Generic.Dictionary(Of Integer, Generic.List(Of Drawing.RectangleF))
If LoadedDocument.FindText("{handtekening}", matchRect) Then
For Each kv As Generic.KeyValuePair(Of Integer, Generic.List(Of Drawing.RectangleF)) In matchRect
Dim loadedpage As PdfLoadedPage = LoadedDocument.Pages(kv.Key)
For Each r As Drawing.RectangleF In kv.Value
intIndex = intIndex + 1
'Dim i As New PdfImage()
'Dim loadedForm As PdfLoadedForm
If signType = SignType.eHealth Or signType = SignType.eID Then
Dim signatureField3 As New PdfSignatureField(loadedpage, "ncSignatureField" & intIndex)
'loadedForm = LoadedDocument.Form
signatureField3.Bounds = New RectangleF(r.X, r.Y - (signatureImage.Height / 2), signatureImage.Width / 2, signatureImage.Height / 2)
LoadedDocument.Form.Fields.Add(signatureField3)
Else
loadedpage.Graphics.DrawImage(signatureImage, r.X, r.Y, signatureImage.Width / 2, signatureImage.Height / 2)
End If
Next
Next
End If
LoadedDocument.Save(memstream)
If signType = SignType.eHealth Or signType = SignType.eID Then
Dim strKeystore As String
Dim strPWD As String
Dim pdfCert As PdfCertificate = Nothing
Select Case signType
Case SignType.eHealth
strKeystore = Solution.GetGlobalDefaultValue("KeystoreDir", "./eHealth/config/P12") & "/" & Solution.GetGlobalDefaultValue("PropertiesEnvironment", "prd") & "/" & Solution.SolutionVariables.EncryptionKeystore
strKeystore = Replace(strKeystore, "/", "\")
strPWD = Solution.SolutionVariables.EncryptionPassword
'Dim certificateStream1 As FileStream = New FileStream(strKeystore, FileMode.Open, FileAccess.Read)
'pdfCert = New PdfCertificate(certificateStream1, strPWD)
pdfCert = New PdfCertificate(strKeystore, strPWD)
Case SignType.eID
pdfCert = Syncfusion.Pdf.Security.PdfCertificate.FindBySerialId(Syncfusion.Pdf.Security.StoreType.MY, System.Convert.FromBase64String(strCertificate))
End Select
Dim signatureField As PdfLoadedSignatureField = Nothing
intIndex = 0
Do
intIndex = intIndex + 1
LoadedDocument = New PdfLoadedDocument(memstream)
Dim page As PdfPageBase = LoadedDocument.Pages(0)
If LoadedDocument.Form.Fields.TryGetField("ncSignatureField" & intIndex, signatureField) Then
Dim signature As PdfSignature
signature = New PdfSignature(LoadedDocument, TryCast(signatureField.Page, PdfLoadedPage), pdfCert, "Signature", signatureField)
'signature.TimeStampServer = New TimeStampServer(New Uri("http://rfc3161timestamp.globalsign.com/advanced"))
'signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges
signature.ContactInfo = Solution.SolutionVariables.MedewerkerTelefoon
signature.LocationInfo = Solution.SolutionVariables.MedewerkerWoonplaats
signature.Reason = "I am author of this document."
signature.EnableLtv = False
'signature.Bounds = signatureField.Bounds
signature.Appearance.Normal.Graphics.DrawImage(signatureImage, 0, 0, signatureField.Bounds.Width, signatureField.Bounds.Height)
'signature.TimeStampServer = New TimeStampServer(New Uri("http://timestamp.entrust.net/TSS/RFC3161sha2TS"))
memstream = New MemoryStream()
LoadedDocument.Save(memstream)
End If
'memstream.Position = 0
Loop While Not IsNothing(signatureField)
Else
LoadedDocument = New PdfLoadedDocument(memstream)
End If
docStream.Close()
LoadedDocument.Save(strPDFFile)
LoadedDocument.Close(True)
Return True
Catch ex As Exception
Solution.ShowError(ex, "Fout in PDFSign.Sign")
Return False
End Try
End If
End Function