At the moment we are evaluating Aspose PDF to digitally sign PDF files. Our client has a USB token, and they don’t have and will not have a PFX file, so we get the certificate from the store (System.Security.Cryptography.X509Certificates.X509Store). We tried three different ways to sign PDF by Aspose PDF, but without any result.
When we use the following code:
Aspose.Words.Document doc = new Aspose.Words.Document(inputDOcFile);
Aspose.Words.Saving.PdfSaveOptions options = new Aspose.Words.Saving.PdfSaveOptions();
options.Compliance = Aspose.Words.Saving.PdfCompliance.PdfA1b;
X509Certificate2 cert = GetCertificate();
options.DigitalSignatureDetails = new Aspose.Words.Saving.PdfDigitalSignatureDetails(cert, "reason", "location", DateTime.Now);
doc.Save(outputPDF, options);
we get the following exception:
“Internal error occurred”:
at System.Security.Cryptography.Pkcs.SignedCms.Sign(CmsSigner signer, Boolean silent)
at System.Security.Cryptography.Pkcs.SignedCms.ComputeSignature(CmsSigner signer, Boolean silent)
at x4f4df92b75ba3b67.x93619359a213c4b3.x73bb2b40010c1ca8(Stream x3f9e1d48f6c754fe, X509Certificate2 x93bf26bc80edc72e, xc102c6e35aff75b1 x4e599cb76e2435b4)
at x4f4df92b75ba3b67.x93619359a213c4b3.xca99e0aebcf34c85(Stream xcf18e5243f8d5fd3, Int32 xa6fa92839a392f8c, X509Certificate2 x0708ffc6efe2b1f3, xc102c6e35aff75b1 x4e599cb76e2435b4)
at x4f4df92b75ba3b67.x0d8cdce10fda1cfd.xa0dfc102c691b11f()
at x4f4df92b75ba3b67.x92faf2a956f0f5a7.xa0dfc102c691b11f()
at x6a671772ec29137f.xcd769e39c0788209.DoEndDocument()
at xf989f31a236ff98c.x6c74398bceb133f8.xa2e0b7f7da663553(x8556eed81191af11 x5ac1382edb7bf2c2)
at Aspose.Words.Document.xf381a641001e6830(Stream xcf18e5243f8d5fd3, String xafe2f3653ee64ebc, SaveOptions xc27f01f21f67608c)
at Aspose.Words.Document.Save(String fileName, SaveOptions saveOptions)
We also tried doing it this way:
PdfFileSignature pdfSign = new PdfFileSignature(inputPDF, outputPDF);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
X509Certificate2 certificate = GetCertificate();
byte[] pkcs10 = certificate.Export(X509ContentType.Pfx, "password");
MemoryStream memStream = new MemoryStream();
BinaryWriter binWriter = new BinaryWriter(memStream);
binWriter.Write(pkcs10);
memStream.Position = 0;
PKCS1 signature = new PKCS1(memStream, "password");
pdfSign.Sign(1, "Signature Reason", "Contact", "Location", false, rect, signature);
pdfSign.Save();
```cs
but we get such exception:
```cs
"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index":
at System.Collections.ArrayList.get_Item(Int32 index)
at . (Byte[] , Stream,String ) )
at . () )
at Aspose.Pdf.InteractiveFeatures.Forms.Signature.Sign(Signature signature)
at Aspose.Pdf.InteractiveFeatures.Forms.SignatureField.Sign(Signature signature, Stream pfx, String pass)
at Aspose.Pdf.InteractiveFeatures.Forms.SignatureField.Sign(Signature signature)
at Aspose.Pdf.Facades.PdfFileSignature.Save(Stream outputStream)
at Aspose.Pdf.Facades.PdfFileSignature.Save(String outputFile)
at Aspose.Pdf.Facades.PdfFileSignature.Save()
```cs
And finally, when we use the same code but removed the PASSWORD parameter:
```cs
byte[] pkcs10 = certificate.Export(X509ContentType.Pfx);//, "password");
res = new PKCS1(memStream); //, "password");
and we get the following exception:
“Certificate file was not provided”:
at Aspose.Pdf.InteractiveFeatures.Forms.SignatureField.Sign(Signature signature)
at Aspose.Pdf.Facades.PdfFileSignature.Save(Stream outputStream)
at Aspose.Pdf.Facades.PdfFileSignature.Save(String outputFile)
at Aspose.Pdf.Facades.PdfFileSignature.Save()
What should we do to make this work?