简体   繁体   中英

Sign a pdf with x509certificate from current user store

first of all please note that I'm quite new to this platform and therefore not used to the rules or "best practices", so please let me know if there's anything i should edit.

But now, here's my problem:

I want to sign a pdf document with a certificate from the current user store. For this task, I'm not allowed to use libraries that require a license, so basically everything i use has to be open source. The private key of the certificate that i have to use for signing is set to unexportable. I already managed to complete the task by using the "Syncfusion PDF" library, but unfortunately, it's not open source.

Since I'm quite new to programming and especially c# i now struggle to find a solution without using Syncfusion. Hope somebody can help:)

Here's the code WITH Syncfusion. it works perfectly fine:

Stream stream = new FileStream(src, FileMode.OpenOrCreate);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;
PdfLoadedSignatureField field = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField;
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
X509Certificate2 digitalID = collection[0];
PdfCertificate certificate = new PdfCertificate(digitalID);
PdfSignature pdf1 = new PdfSignature(loadedDocument, page, certificate, "Signature", field);
field.Signature = pdf1;
loadedDocument.Save(stream);
loadedDocument.Close(true);

We have analyzed your requirement. But we can only help if you have any doubts in Syncfusion library.

Note : I work for Syncfusion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM