Signer
内容
[
隐藏
]SignatureLineOptions.Signer property
获取或设置签名行的建议签名者。 此属性的默认值为空字符串(Empty).
public string Signer { get; set; }
例子
演示如何向文档添加签名行,然后使用数字证书对其进行签名。
[Description("WORDSNET-16868")]
public static void Sign()
{
string signeeName = "Ron Williams";
string srcDocumentPath = MyDir + "Document.docx";
string dstDocumentPath = ArtifactsDir + "SignDocumentCustom.Sign.docx";
string certificatePath = MyDir + "morzal.pfx";
string certificatePassword = "aw";
CreateSignees();
Signee signeeInfo = mSignees.Find(c => c.Name == signeeName);
if (signeeInfo != null)
SignDocument(srcDocumentPath, dstDocumentPath, signeeInfo, certificatePath, certificatePassword);
else
Assert.Fail("Signee does not exist.");
}
/// <summary>
/// 创建使用提供的签名者信息和 X509 证书签名的源文档的副本。
/// </summary>
private static void SignDocument(string srcDocumentPath, string dstDocumentPath,
Signee signeeInfo, string certificatePath, string certificatePassword)
{
Document document = new Document(srcDocumentPath);
DocumentBuilder builder = new DocumentBuilder(document);
// 配置并插入签名行,这是文档中的一个对象,将显示我们用来签名的签名。
SignatureLineOptions signatureLineOptions = new SignatureLineOptions
{
Signer = signeeInfo.Name,
SignerTitle = signeeInfo.Position
};
SignatureLine signatureLine = builder.InsertSignatureLine(signatureLineOptions).SignatureLine;
signatureLine.Id = signeeInfo.PersonId;
// 首先,我们将保存文档的未签名版本。
builder.Document.Save(dstDocumentPath);
CertificateHolder certificateHolder = CertificateHolder.Create(certificatePath, certificatePassword);
SignOptions signOptions = new SignOptions
{
SignatureLineId = signeeInfo.PersonId,
SignatureLineImage = signeeInfo.Image
};
// 用使用证书签名的版本覆盖我们上面保存的未签名文档。
DigitalSignatureUtil.Sign(dstDocumentPath, dstDocumentPath, certificateHolder, signOptions);
}
#if NET48 || JAVA
/// <summary>
/// 将图像转换为字节数组。
/// </summary>
private static byte[] ImageToByteArray(Image imageIn)
{
using (MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
}
#endif
public class Signee
{
public Guid PersonId { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public byte[] Image { get; set; }
public Signee(Guid guid, string name, string position, byte[] image)
{
PersonId = guid;
Name = name;
Position = position;
Image = image;
}
}
private static void CreateSignees()
{
mSignees = new List<Signee>
{
#if NET48 || JAVA
new Signee(Guid.NewGuid(), "Ron Williams", "Chief Executive Officer",
ImageToByteArray(Image.FromFile(ImageDir + "Logo.jpg"))),
#elif NET5_0_OR_GREATER || __MOBILE__
new Signee(Guid.NewGuid(), "Ron Williams", "Chief Executive Officer",
SkiaSharp.SKBitmap.Decode(ImageDir + "Logo.jpg").Bytes),
#endif
#if NET48 || JAVA
new Signee(Guid.NewGuid(), "Stephen Morse", "Head of Compliance",
ImageToByteArray(Image.FromFile(ImageDir + "Logo.jpg")))
#elif NET5_0_OR_GREATER || __MOBILE__
new Signee(Guid.NewGuid(), "Stephen Morse", "Head of Compliance",
SkiaSharp.SKBitmap.Decode(ImageDir + "Logo.jpg").Bytes)
#endif
};
}
private static List<Signee> mSignees;
也可以看看
- class SignatureLineOptions
- 命名空间 Aspose.Words
- 部件 Aspose.Words