使用iTextSharp在C#中向表单添加签名字段

时间:2011-12-17 23:58:10

标签: c# itextsharp

我正在寻找使用winforms使用itextsharp在PDF表单中添加签名字段 以下是我试过的代码。

已编辑

var writer = new PdfWriter(outputPdfStream);
PdfFormField SigField = PdfFormField.CreateSignature(writer);

运行第二行时会抛出以下错误:

Error   1   'iTextSharp.text.pdf.PdfWriter' does not contain a constructor that takes 1 arguments

我还在学习iTextsharp的输入和输出。任何指导都非常感谢。

谢谢!

1 个答案:

答案 0 :(得分:3)

错误告诉您iTextSharp.text.pdf.PdfWriter没有构造函数只接受一个参数。

How to sign a PDF using iText and iTextSharp

KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream("my_private_key.pfx"), "my_password".toCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey key = (PrivateKey)ks.getKey(alias, "my_password".toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
PdfReader reader = new PdfReader("original.pdf");

FileOutputStream fout = new FileOutputStream("signed.pdf");
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');

PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.setReason("I'm the author");
sap.setLocation("Lisbon");
// comment next line to have an invisible signature
sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
stp.close();