I’m using Aspose.Words v16.6.0.0 and created a document by code then save as PDF with signature https://docs.aspose.com/words/net/working-with-digital-signatures/.
The PDF file was successfully generated but when the file is opened in Acrobat Reader, it complains about the signature is invalid (“signature byte range is invalid”).
I’ve tried to sign the PDF file using iTextSharp with the same set of certificate without problems.
Hi there,
Thanks for your inquiry. Could you please attach your input Word document along with certificate used to instantiate X509Certificate2 here for testing? We will investigate the issue on our side and provide you more information.
Hi,
The word document is generated on-the-fly using the code below that contains 1 line only. Please refer to the attached file for the complete set of self-signed test certificate and CA.
The certificate file password is P@ssw0rd.
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
builder.Writeln(string.Format("Digitally Signed ({0})", DateTime.Now.ToLocalTime().ToString("o")));
Hi there,
Thanks for sharing the detail. We have tested the scenario using following code example and have not found the shared issue. We have attached the output document with this post for your kind reference. Please use following code example at your end and let us know if you still face problem. Hope this helps you.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Test Signed PDF.");
PdfSaveOptions options = new PdfSaveOptions();
options.DigitalSignatureDetails = new PdfDigitalSignatureDetails(CertificateHolder.Create(MyDir + @"crypto.lab.net.usr.pfx", "P@ssw0rd"), "Test Signing", "Aspose Office", DateTime.Now);
doc.Save(MyDir + "Out v16.6.0.pdf", options);
Thanks for your information.
Found the issue was caused by saving to memory stream that is nothing wrong with Aspose.Words.
However, I have one more question about loading the X509Certificate2 into the CertificateHolder object. My certificate would be loaded from the Windows certificate store instead of from file system as below. The highlighted line code is marked as obsolete by Aspose.Words. Is there any drop-in replacement for that?
// open the certificate store
X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certificateStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificates = certificateStore.Certificates;
X509Certificate2 certificate = certificates.Find(X509FindType.FindBySubjectDistinguishedName,
fullQualifiedName, true)
.OfType()
.FirstOrDefault();
CertificateHolder holder = CertificateHolder.Create(certificate);
Hi there,
Thanks for your inquiry. In your case, we suggest you please use CertificateHolder.Create method (Byte[], SecureString). Hope this helps you.