SignIncremental.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.AcroForms;
using GrapeCity.Documents.Text;
using System.Security.Cryptography.X509Certificates;
namespace DsPdfWeb.Demos
{
// 此示例生成并签署 PDF(使用与 SignDoc 示例类似的代码),
// 然后使用第二个签名对生成的 PDF 进行签名,而不会使原始文件失效
// 签名,通过使用增量更新(使用 Sign() 方法时的默认设置)。
public class SignIncremental
{
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
// 加载签名文档(我们使用类似于SignDoc示例的代码):
doc.Load(CreateAndSignPdf());
// 初始化第二个证书:
var pfxPath = Path.Combine("Resources", "Misc", "JohnDoe.pfx");
var cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "secret",
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
var sp2 = new SignatureProperties()
{
SignatureBuilder = new Pkcs7SignatureBuilder()
{
CertificateChain = new X509Certificate2[] { cert }
},
Location = "DsPdfWeb Demo Browser",
SignerName = "DsPdfWeb",
SigningDateTime = Common.Util.TimeNow(),
};
// 找到第二个(尚未填写)签名字段:
var sfld2 = doc.AcroForm.Fields["SecondSignature"] as SignatureField;
// 连接签名字段和签名属性:
sp2.SignatureField = sfld2 ?? throw new Exception("Unexpected: could not find 'SecondSignature' field");
// 签署并保存文档:
doc.Sign(sp2, stream);
// 倒回流以读取刚刚创建的文档
// 进入另一个 GcPdfDocument 并验证所有签名:
stream.Seek(0, SeekOrigin.Begin);
var doc2 = new GcPdfDocument();
doc2.Load(stream);
foreach (var fld in doc2.AcroForm.Fields)
if (fld is SignatureField sfld)
if (!sfld.Value.VerifySignatureValue())
throw new Exception($"Failed to verify signature for field {sfld.Name}");
// 完成(生成并签名的文档已保存到“流”)。
return doc.Pages.Count;
}
// 这个方法与SignDoc示例几乎完全相同,
// 但添加了第二个签名字段(但不签名):
private Stream CreateAndSignPdf()
{
var doc = new GcPdfDocument();
var page = doc.NewPage();
var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 14 };
page.Graphics.DrawString("你好世界!" +
"",
tf, new PointF(72, 72));
// 初始化测试证书:
var pfxPath = Path.Combine("Resources", "Misc", "DsPdfTest.pfx");
var cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "qq",
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
var sp = new SignatureProperties()
{
SignatureBuilder = new Pkcs7SignatureBuilder()
{
CertificateChain = new X509Certificate2[] { cert }
},
Location = "DsPdfWeb Demo Browser",
SignerName = "DsPdfWeb",
SigningDateTime = Common.Util.TimeNow(),
};
// 初始化一个签名字段来保存签名:
var sf = new SignatureField();
sf.Widget.Rect = new RectangleF(72, 72 * 2, 72 * 4, 36);
sf.Widget.Page = page;
sf.Widget.BackColor = Color.LightSeaGreen;
// 将签名字段添加到文档中:
doc.AcroForm.Fields.Add(sf);
// 连接签名字段和签名属性:
sp.SignatureField = sf;
// 添加第二个签名字段:
var sf2 = new SignatureField() { Name = "SecondSignature" };
sf2.Widget.Rect = new RectangleF(72, 72 * 3, 72 * 4, 36);
sf2.Widget.Page = page;
sf2.Widget.BackColor = Color.LightYellow;
// 将签名字段添加到文档中:
doc.AcroForm.Fields.Add(sf2);
var ms = new MemoryStream();
doc.Sign(sp, ms);
return ms;
}
}
}