Everything works rather well in .NET Framework, but not in .NET Core.
This is the program used in .NET Framework, and works perfectly:
- Code: Select all
using (PdfDocument document = new PdfDocument())
{
document.LoadFromFile(@"C:\input.pdf");
using (X509Store store = new X509Store(StoreLocation.CurrentUser))
{
store.Open(flags: OpenFlags.ReadOnly);
X509Certificate2Collection storecollection = X509Certificate2UI.SelectFromCollection(certificates: store.Certificates, title: null, message: null, selectionFlag: X509SelectionFlag.SingleSelection);
using (PdfCertificate cert = new PdfCertificate(signData: storecollection[0].RawData))
{
PdfSignature signature = new PdfSignature(document, document.Pages[0], cert, "signature0")
{
DateLabel = "Date:",
Date = DateTime.Now,
DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges,
Certificated = true
};
document.FileInfo.IncrementalUpdate = false;
document.CompressionLevel = PdfCompressionLevel.Best;
document.SaveToFile(@"C:\output.pdf");
document.Close();
}
}
}
The above code doesn't work in .NET Core:
- Out of the bat, the .NET Core constructor is different, has an extra string argument called password.
- That argument doesn't do anything. If one inserts null or even the certificate password (ouch!), it still will not sign the certificate.
- On .NET Framework after I select the certificate there's a call from Pdfcertificate to insert the pin. That does not occur in .NET Core, as it crashes with a PdfException: lost private key.
Am I doing something wrong? Thanks in advance.