Reputation: 61
Some plugins and programs can do this; is there any example or tutorial how to do this using .net and itextSharp??
I need to take a signed pdf and stamp an imagen on all pages and also add another signature. The first signature on the document must be valid when open the file.
Thanks in advance.
Upvotes: 3
Views: 7059
Reputation: 11
Public Function sign(keystore As String, level As Integer, src As String, name As String, dest As String, sig As String, pass As String)
' Try
'Dim store As System.Security.Cryptography.X509Certificates.X509Store = New System.Security.Cryptography.X509Certificates.X509Store
'store.Open(System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly)
'Dim sel As System.Security.Cryptography.X509Certificates.X509Certificate2Collection
' If sig <> "" And pass <> "" Then
Try
Dim y As Int16 = 200
' For i As Integer = 0 To sel.Count - 1
Dim pdfReader As PdfReader = New PdfReader(src)
Dim signedPdf = New FileStream(dest, FileMode.Create)
Try
Dim cert As X509Certificate2 = New X509Certificate2(sig, pass)
Dim cp As Org.BouncyCastle.X509.X509CertificateParser = New Org.BouncyCastle.X509.X509CertificateParser()
Dim chain As Org.BouncyCastle.X509.X509Certificate() = New Org.BouncyCastle.X509.X509Certificate() {cp.ReadCertificate(cert.RawData)}
Dim stamper As PdfStamper
stamper = PdfStamper.CreateSignature(pdfReader, signedPdf, "0"c, Nothing, True)
Dim signatureAppearance As PdfSignatureAppearance = stamper.SignatureAppearance
'signatureAppearance.SignatureGraphic = Image.GetInstance(pathToSignatureImage)
signatureAppearance.SetVisibleSignature(name)
signatureAppearance.CertificationLevel = level
Dim externalSignature As IExternalSignature = New X509Certificate2Signature(cert, "SHA-1")
' Dim digest As IExternalSignature = New BouncyCastleDigest
' signatureAppearance.s
'signatureAppearance.SetVisibleSignature(New Rectangle(50,50,50,
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.NAME_AND_DESCRIPTION
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, Nothing, Nothing, Nothing, 0, CryptoStandard.CADES)
' MakeSignature.
' End If
' Catch ex As Exception
'MsgBox(ex.Message)
' End Try
Catch ex As Exception
MsgBox("Signature File Password is not correct for the user Id :" & error_userid)
'Exit Function
End Try
Catch ex As Exception
End Try
Return 0
End Function
Upvotes: 1
Reputation: 95898
When you require that the first signature on the document must be valid when open the file, I assume you foremost talk about opening in a current Adobe Reader version.
In that case you first of all must be aware that the allowed and disallowed changes depend on the first signature itself. Have a look at this answer for information on "Allowed and disallowed changes" as considered by Adobe since version 9 of its Acrobat & Reader.
Thus, to take a signed pdf and stamp an image on all pages and also add another signature and not invalidate (in the eyes of Adobe software) the initial signature in the process, that signed pdf must
either be uncertified (i.e. the initial signature is merely an approval signature)
or be Certified with annotations, form fill-in, and digital signatures, allowed (i.e. the initial signature is a certification signature with those types of changes allowed) and have an empty signature field for you to put your signature into.
If that signed pdf already contains multiple signatures, neither of them by means of Locking information shall have added any additional restrictions.
(Furthermore I assume the PDF neither is encrypted nor does contain dynamic XFA forms; that would complicate things even more.)
If these requirements are fulfilled, you can use a PdfStamper
you retrieve from the static PdfStamper
method
public static PdfStamper CreateSignature(PdfReader reader, Stream os, char pdfVersion, string tempFile, bool append)
where the final bool
parameter value must be true
(i.e. you must work in append mode to not invalidate the initial signature).
Using this PdfStamper
you add your image on the document pages by means of annotations. The iText in Action — 2nd Edition sample TimetableAnnotations3.java / TimetableAnnotations3.cs illustrates how to add annotations with PdfStampers.
Then you add your signature as usual, either creating a new signature field or using an empty one. You find details on the signing process in the whitepaper Digital Signatures for PDF documents; C# samples equivalent to the Java samples in that white paper can be found here on sourceforge.
PS: I always stressed in the eyes of Adobe software in this post. When talking about certified PDFs Adobe is pretty near to the PDF standard here. For signed but uncertified PDFs Adobe applies rules analogous to those specified for certified documents.
Upvotes: 8