We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PdfFileSignature.Save throws "Doesn't match conditions" exception

Hi,


I’m trying to sign an existing pdf with a certificate. Here is my code:

X509Certificate2 objCert = FindCertBySubjectName(“Test subject name”);
string inFile = @“X:\Test\PdfFile.pdf”;
string outFile = @“X:\Test\PdfFile-signed.pdf”;
byte[] pkcs10 = objCert.Export(X509ContentType.Pfx, “1111”);
MemoryStream memStream = new MemoryStream();
BinaryWriter binWriter = new BinaryWriter(memStream);
binWriter.Write(pkcs10);
memStream.Position = 0;
PKCS1 sig = new PKCS1(memStream, “1111”);
sig.Reason = “Reason”;
sig.ContactInfo = “MPPF”;
sig.Location = “Barcelona”;
sig.Date = DateTime.Now;
PdfFileSignature pdfSign = new PdfFileSignature(inFile, outFile);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
pdfSign.Sign(1, true, rect, sig);
pdfSign.Save();

The pdfSign.Save() code throws an exception that says “Doesn’t match conditions”. Any idea what can be wrong and make this exception being thrown?

PS: The certificate I’m using is valid and is being readed from a local store.

Thank you!


Hi Jacobo,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for sharing the sample code.

Which version of Aspose.Pdf for .NET are you using? Please download and try the latest version of Aspose.Pdf for .Net v6.5. If you still face the problem, please share your template PDF file and the certificate file with us. We will look into your issue.

Thank You & Best Regards,

Hi Nausherwan,

Thank you for your answer. I was using the Aspose.Pdf for .Net v6.4. But I 've downloaded the newest version (v6.5) and still facing the same problem. I would give you more information about the exception, but "Doesn't match conditions" is the only readable part I get.

I've attached the files you've asked for, and a txt file with the StrackTrace, so I hope it can help you find why the code is throwing this exception.

Thank you!

Hi Jacobo,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for sharing the template PDF file and Certificate File.

I used the following sample code with Aspose.Pdf for .NET v6.6 and I am able to sign the documents properly (attached is the resultant file). Please try the latest version and if you still face any issue, please create a sample application (as current code shared by you is missing method) and share with us. We will check it and get back to you soon.

// Read the certificate from the store

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

StorePermission sp = new StorePermission(PermissionState.Unrestricted);

sp.Flags = StorePermissionFlags.OpenStore;

sp.Assert();

//// Open the store

store.Open(OpenFlags.OpenExistingOnly | OpenFlags.MaxAllowed);

// Only interested in valid certificates

X509Certificate2Collection certCollection = store.Certificates.Find(X509FindType.FindBySerialNumber,

" 00 bb 00 00 00 00 00 00 01", false);

X509Certificate2Enumerator enumerator = certCollection.GetEnumerator();

X509Certificate2 objCert = null;

while (enumerator.MoveNext())

{

objCert = enumerator.Current;

break;

}

bool check = objCert.HasPrivateKey;

byte[] pfxCertificate = objCert.Export(X509ContentType.Pkcs12, "1111");

string inFile = @"D:\AP Data\SigningTests.pdf";

string outFile = @"D:\AP Data\SigningTests-signed.pdf";

byte[] pkcs10 = objCert.Export(X509ContentType.Pfx, "1111");

MemoryStream memStream = new MemoryStream();

BinaryWriter binWriter = new BinaryWriter(memStream);

binWriter.Write(pkcs10);

memStream.Position = 0;

PKCS1 sig = new PKCS1(memStream, "1111");

sig.Reason = "Reason";

sig.ContactInfo = "MPPF";

sig.Location = "Barcelona";

sig.Date = DateTime.Now;

PdfFileSignature pdfSign = new PdfFileSignature(inFile, outFile);

System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);

pdfSign.Sign(1, true, rect, sig);

pdfSign.Save();

Also, please make sure that key must be exportable when importing certificate into local store.

Thank You & Best Regards,