DigitalSignatureCollection

DigitalSignatureCollection class

提供附加到文档的数字签名集合。

public class DigitalSignatureCollection : IEnumerable

构造函数

姓名描述
DigitalSignatureCollection()DigitalSignatureCollection 的构造函数.

方法

姓名描述
Add(DigitalSignature)向 DigitalSignatureCollection 添加一个签名。
GetEnumerator()获取 DigitalSignatureCollection 的枚举器, 此枚举器允许对集合进行迭代

例子

下面的例子展示了如何创建数字签名

[C#]
internal void ValidateSignature()
{
   Workbook wb = new Workbook(@"newfile.xlsx");
   //wb.IsDigitallySigned 在工作簿已签名时为真。
   System.Console.WriteLine(wb.IsDigitallySigned);
   //从工作簿中获取数字签名集合
   DigitalSignatureCollection dsc = wb.GetDigitalSignature();
   foreach (DigitalSignature ds in dsc)
   {
       System.Console.WriteLine(ds.Comments);
       System.Console.WriteLine(ds.SignTime);
       System.Console.WriteLine(ds.IsValid);
   }
}
internal void SignSignature()
{
   //dsc 是签名集合,包含一个或多个签名需要签名
   DigitalSignatureCollection dsc = new DigitalSignatureCollection();
   //证书必须包含私钥,可以从证书文件或windows证书集合中构造。
   //123456是证书的密码
   X509Certificate2 cert = new X509Certificate2("mykey2.pfx", "123456");
   DigitalSignature ds = new DigitalSignature(cert, "test for sign", DateTime.Now);
   dsc.Add(ds);
   Workbook wb = new Workbook();
   //wb.SetDigitalSignature 对 dsc 中的所有签名进行签名
   wb.SetDigitalSignature(dsc);
   wb.Save(@"newfile.xlsx");
}

[Visual Basic]
   Sub ValidateSignature()
   Dim workbook As Workbook = New Workbook("newfile.xlsx")
   '当工作簿已签名时,Workbook.IsDigitallySigned 为真。
   System.Console.WriteLine(workbook.IsDigitallySigned)
   '从工作簿中获取 digitalSignature 集合
   Dim dsc As DigitalSignatureCollection = workbook.GetDigitalSignature()
   Dim ds As DigitalSignature
   For Each ds In dsc
       System.Console.WriteLine(ds.Comments)
       System.Console.WriteLine(ds.SignTime)
       System.Console.WriteLine(ds.IsValid)
   Next
End Sub

Sub SignSignature()
   'dsc 是签名集合,包含签名所需的一个或多个签名
   Dim dsc As DigitalSignatureCollection = New DigitalSignatureCollection()
   'cert 必须包含私钥,它可以从 cert 文件或 windows 证书集合中构造。
   Dim cert As X509Certificate2 = New X509Certificate2("mykey2.pfx", "123456")
   '创建带有证书、签名目的和签名时间的签名
   Dim ds As DigitalSignature = New DigitalSignature(cert, "test for sign", DateTime.Now)
   dsc.Add(ds)
   Dim workbook As Workbook = New Workbook()
   'workbook.SetDigitalSignature 签署 dsc 中的所有签名
   workbook.SetDigitalSignature(dsc)
   workbook.Save("newfile.xlsx")
End Sub

也可以看看