所以我需要通过webform从用户的输入中签名PDF,我在这里的iTextSharp教程中遇到了SignEmptyField示例:
我稍微修改了以下内容。
public class C2_03_SignEmptyField
{
public char[] PASSWORD = "test".ToCharArray();
public String SRC = ConfigurationManager.AppSettings["pdfSource"];
public String DEST = ConfigurationManager.AppSettings["pdfDest"];
public void Sign(String src, String name, String dest, ICollection<X509Certificate> chain, ICipherParameters pk,
String digestAlgorithm, CryptoStandard subfilter,
String reason, String location)
{
// Creating the reader and the stamper
PdfReader reader = new PdfReader(src);
PdfReader.unethicalreading = true;
AcroFields fields = reader.AcroFields;
FileStream os = new FileStream(dest, FileMode.Create);
PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0');
// Creating the appearance
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
appearance.Reason = reason;
appearance.Location = location;
appearance.Layer2Text = "Ben Ziegler"; // SignatureTextBox.Text;
appearance.Layer2Font = new Font(Font.FontFamily.COURIER, 7.0f, Font.NORMAL, BaseColor.LIGHT_GRAY);
appearance.Acro6Layers = true;
appearance.SetVisibleSignature(name);
// Creating the signature
IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm);
//ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, subfilter);
}
public void Main()
{
Pkcs12Store store = new Pkcs12Store(new FileStream(@"D:\test.pfx", FileMode.Open), PASSWORD);
String alias = "";
ICollection<X509Certificate> chain = new List<X509Certificate>();
// searching for private key
foreach (string al in store.Aliases)
{
if (store.IsKeyEntry(al) && store.GetKey(al).Key.IsPrivate)
{
alias = al;
break;
}
}
AsymmetricKeyEntry pk = store.GetKey(alias);
foreach (X509CertificateEntry c in store.GetCertificateChain(alias))
{
chain.Add(c.Certificate);
}
RsaPrivateCrtKeyParameters parameters = pk.Key as RsaPrivateCrtKeyParameters;
C2_03_SignEmptyField app = new C2_03_SignEmptyField();
app.Sign(SRC, "Digital Signature", String.Format(DEST, 1), chain, parameters, DigestAlgorithms.SHA256,
CryptoStandard.CMS, "Test 1", "Ghent");
}
}
我添加了appearance.Layer2Text和appearance.Layer2Font的东西,好像来自文本框。它没有错误,但签名也没有显示在签名字段中,因为我没有收到任何错误,也没有显示,所以不知道从哪里开始。
是否有不同的方式来签署空的现有签名字段?