Hi, i am new in this forum... i am a java programmer trying to do something with c# .net, in this case i am trying to sign multiple pdf files using iTextSharp. This is the code:
- public void sign()
- {
- Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
- Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(this.getCertificate().RawData) };
- bool bHasPrivateKey;
- bHasPrivateKey = this.getCertificate().HasPrivateKey;
- if (!bHasPrivateKey)
- {
- MessageBox.Show("El certificado seleccionado no es válido para firmar digitalmente.");
- return;
- }
- PdfReader reader = new PdfReader(this.getOriginalFileName());
- FileStream signedFileStream = new FileStream(this.getSignedFileName(), FileMode.Create);
- PdfStamper stp = PdfStamper.CreateSignature(reader, signedFileStream, '\0', null, true);
- sap = stp.SignatureAppearance;
- appearanceHandler.setAppearanceStyleTo(sap, appearanceStyle, new System.Drawing.Rectangle(100, 100, 200, 300));
- sap.SetCrypto(null, chain, null, null);
- sap.Location = PdfPKCS7.GetSubjectFields(chain[0]).GetField("L");
- sap.Contact = this.contactInfo;
- sap.Reason = this.getReason();
- sap.Acro6Layers = true;
- PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_SHA1);
- dic.Date = new PdfDate(sap.SignDate);
- dic.Name = PdfPKCS7.GetSubjectFields(chain[0]).GetField("CN");
- if (sap.Reason != null)
- dic.Reason = sap.Reason;
- if (appearanceHandler.showLocation)
- dic.Location = sap.Location;
- if (appearanceHandler.showContact)
- dic.Contact = sap.Contact;
- sap.CryptoDictionary = dic;
- int csize = 15000;
- Hashtable exc = new Hashtable();
- exc[PdfName.CONTENTS] = csize * 2 + 2;
- sap.PreClose(exc);
- try
- {
- RSACryptoServiceProvider rsa = (RSACryptoServiceProvider) this.certificate.PrivateKey;
- SHA1Managed sha = new SHA1Managed();
- byte[] hashedData = sha.ComputeHash(sap.RangeStream);
- byte[] sig = rsa.SignHash(hashedData, CryptoConfig.MapNameToOID("SHA1"));
- bool verified = rsa.VerifyHash(hashedData, "SHA1", sig);
- MessageBox.Show("Verified: " + verified);
- byte[] outc = new byte[csize];
- PdfDictionary dic2 = new PdfDictionary();
- Array.Copy(sig, 0, outc, 0, sig.Length);
- dic2.Put(PdfName.CONTENTS, new PdfString(outc).SetHexWriting(true));
- sap.Close(dic2);
- signedFileStream.Close();
- }
- catch (CryptographicException ce)
- {
- MessageBox.Show(ce.Message);
- }
- }
I iterate the sign method one time for each pdf file.
I use a private CSP instead the Microsoft Base CSP, because i need to do a massive signature of files, when i tried to do that with the Microsoft CSP, the application hangs and throws a Cryptographic Exception, the only way that i found to get it works, was to use the CSP provided by the token manufacturer.
When i used the Microsoft CSP with CMSSigner, everything, except the massive signature, went well... when i signed a single pdf file, the signature was verified correct by adobe reader. But now with this code, using the Safenet CSP,the massive signature work fine but all signatures are verified wrong... the message that acrobat reader throws is: "Error decoding BER:".
I do not know what can i do for solve this. Any Ideas on what could be happen?,
Thanks in advance,
Juan Manuel Diaz

Roei BarRank #1,271 · 1.4K reputationPosted Sep 14, 2009, 12:37 PM EDT
its a bit hard to help cause i cant see the entire picture,
would you like to send me the project to my personal Email
[email protected]
so i can have a look @ it and see the problem, cause the code seems perfectly OK
u
Juan Manuel Diaz13 reputationPosted Sep 14, 2009, 5:03 PM EDT
Best Regards,
Juan Manuel Diaz
Juan Manuel Diaz13 reputationPosted Sep 16, 2009, 6:39 AM EDT
Best Regards!!!,
Juan Manuel
Xavier Cusi6 reputationPosted Dec 23, 2009, 4:55 AM EST
I've the same problem, i need to sign a lot of pdf documents and i wan
I have the same problem. I need to sign many pdf documents and pass the pin with a variable, because I have to do it in a batch process.
Have you had any response? You could solve your problem? appreciate any help.
Thank you very much in advance
Xavi Cusí Oliveras.
Gabrielius Liaškus6 reputationPosted Jun 1, 2015, 12:15 AM EDT
Ram Prakash170 reputationPosted Jul 16, 2020, 1:44 AM EDT
Sapna GirishRank #1,810 · 819 reputationPosted Jul 19, 2020, 4:00 PM EDT
Marvin ReidRank #1,963 · 746 reputationPosted Sep 15, 2023, 3:20 PM EDT
A solution you can look into for signing PDFs is by leveraging the PDFFile SignDocument method. Here is a code snippet and a tutorial you can use:
static void SignPDFDocument(string inputPdf, string outputPdf, string signatureFile, string password = null) { try { PDFFile inputDoc = new PDFFile(inputPdf); inputDoc.SignDocument(outputPdf, signatureFile, password); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } Console.WriteLine("Signed PDF successfully"); }https://www.leadtools.com/help/sdk/tutorials/dotnet-fx-console-add-a-digital-signature-to-a-pdf.html