Management appearance

Hello I would kindly have some clarification about the processing of PDF files that contain signature fields.

I’m developing an application graphometric signature using
My application will be able to read the special characters (for example, #sig#) in PDF files to be signed and thanks to these characters will create one or more signature fields with its appearance, so that the user is able to immediately show the fields to be signed.After signing replace the appearance with the image of the user’s signature. Well, when I create the appearance my application not working properly, and if the comments of the creation code looks like everything functions properly.

The first question is: when I create the appearance, I must also save the file? If yes by what mode? I know that every time you make a signature The pdf file must be saved in incremental mode, you have to do this even when you add annotations?

The second question is: after signing, just call the setAppearance method to replace the old, or you must first delete it? If you must delete it can you provide me a code example?

The third question is the signature image is a file.png who is transformed into a stream and then passed to the method pdftron.PDF.Image.Create (doc, imagefile). This can cause malfunctions? How can I get a better picture? I have to convert the image to RGB? Can you provide me a code example?

Here’s the code to create the appearance:
Dim sig As Field = pdfdoc.FieldCreate("sig" & paginaOld.ToString & “_” & contaFirme.ToString, Field.Type.e_signature)
Dim annotTypeWidget As pdftron.PDF.Annots.Widget = pdftron.PDF.Annots.Widget.Create(_pdfdoc.GetSDFDoc, New Rect(bbox.x1, bbox.y1 + 10, bbox.x1 + 150, bbox.y1 + 70), sig)
annotTypeWidget.SetAppearance(CreateSignatureAppearance(_pdfdoc, Nothing))


Private Function CreateSignatureAppearance(ByVal doc As PDFDoc, ByVal fileStream As FileStream) As Obj
Dim w As ElementWriter = New ElementWriter()
Dim b As ElementBuilder = New ElementBuilder()
Dim img As pdftron.PDF.Image = pdftron.PDF.Image.Create(doc, imagefile)
Dim width As Integer = img.GetImageWidth()
Dim height As Integer = img.GetImageHeight()
Dim img_element As Element = b.CreateImage(img, 0, 0, width, height)
Dim stm As Obj = w.End()
’ Set the bounding box
stm.PutRect(“BBox”, 0, 0, width, height)
stm.PutName(“Subtype”, “Form”)
stm.PutName(“Type”, “XObject”)


Return stm

End Function

Below the code that makes signing and replaces the old appearance:
Dim sigHandlerId As SignatureHandlerId = _pdfdoc.AddStdSignatureHandler(“path file.pfx”, “password”)


Dim sigField As Field = _pdfdoc.GetField(sigName)
Dim widgetAnnot As Widget = New Widget(sigField.GetSDFObj)

Dim sigDict As Obj = sigField.UseSignatureHandler(sigHandlerId)
’ Add more information to the signature dictionary
sigDict.PutName(“SubFilter”, “adbe.pkcs7.detached”)
sigDict.PutString(“Name”, sigObj.Who)
sigDict.PutString(“Data”, sigObj.When)
sigDict.PutString(“Reason”, sigObj.Why)
sigDict.PutString(“M”, sigObj.When.ToString())

widgetAnnot.SetAppearance(CreateSignatureAppearance(_pdfdoc, fileStream))


'Save current version
Dim stream As MemoryStream = New MemoryStream(_pdfdoc.Save(0))

I apologize for the post a bit 'long and thank you in advance for the attention that I turn aside.

Best regards

There was an issue digitally saving to a memory stream. Please let me know what .Net framework version you are targeting, and if you need 32 and/or 64bit versions.

All your code looks fine.