我遇到了标题中提到的方法的问题。如果我试图签署XPS文档,它会遇到NullReferenceException:
位于System.Windows.Xps.Packaging.XpsDocument.get_IsSignable((在System.Windows.Xps.Packaging.XpsDocument.SignDigitally(X509Certificate证书,布尔嵌入证书,XpsDigSigPartAlteringRestrictions限制,字符串签名Id,布尔测试IsSignable(在System.Windows.Xps.Packaging.XpsDocument.SignDigitally上(X509Certificate证书、布尔嵌入证书、XpsDigSigPartAlteringRestrictions限制(
任何建议都将不胜感激。
if (sign_digitally)
{
UserCertificateSelector selector = new UserCertificateSelector();
if (selector.ShowDialog() == true)
{
var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
#if DEBUG
X509CertificateCollection collection = certStore.Certificates;
#else
X509CertificateCollection collection = certStore.Certificates.Find(X509FindType.FindByThumbprint, selector.SelectedCertificate.CertificateThumbPrint,true);
#endif
if (collection.Count > 0)
{
xpsDoc.SignDigitally(collection[0], true, XpsDigSigPartAlteringRestrictions.None);
}
}
}
...
请确保处理的是XPS文件格式,而不是OXPS。两者都可以作为XpsDocument打开,但只能对XPS进行签名。您可以查看";XpsDocument.IsSignable";标志一旦打开文件-对于OXPS,它将为null。