PDF 数字签名:使用 iTextSharp 自定义外观
PDF digital signing: customize appearance by using iTextSharp
我正在尝试使用 iTextSharp 5.5.9 签署 pdf 文件。自定义外观时,我收到一个无法理解的错误。这是我所做的
签名方式:
public void Sign1(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);
FileStream os = new FileStream(dest, FileMode.Create);
PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '[=10=]');
// Creating the appearance
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
appearance.Reason = reason;
appearance.Location = location;
appearance.SetVisibleSignature(name);
// Custom text and custom font
appearance.Layer2Text = "This document was signed by ABC";
appearance.Layer2Font = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN);
// Creating the signature
IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm);
MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, subfilter);
}
以及我签名的方式
String KEYSTORE = "C:/Users/user/Desktop/temp.pfx";
char[] PASSWORD = "blabla".ToCharArray();
String SRC = "C:/Users/user/abc.pdf";
String DEST = "C:/Users/user/Desktop/aaa.pdf";
Pkcs12Store store = new Pkcs12Store(new FileStream(KEYSTORE, 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;
Sign1(SRC, "Signature1", String.Format(DEST, 1), chain, parameters, DigestAlgorithms.SHA256, CryptoStandard.CMS, "Customize apprearance", "Blabla");
我收到的消息:
如何解决?
an error which is unintelligible
相反,它明确指出某些参数值不合适,附加信息甚至指出哪个参数值和原因:
The field Signature1 does not exist.
"Signature1" 是您用作签名字段名称的值,您的 Sign1
方法在此处使用它:
appearance.SetVisibleSignature(name);
您调用的方法记录在如下来源中:
/**
* Sets the signature to be visible. An empty signature field with the same name must already exist.
* @param fieldName the existing empty signature field name
*/
virtual public void SetVisibleSignature(String fieldName)
但您的 PDF 似乎还没有同名的空签名字段,这会触发您观察到的异常。
如果您想签署(使用签名的文档内可视化)PDF 但不能使用现有的空签名字段,您必须使用该方法的不同重载,您可以向该方法提供必要的信息来创建签名字段可视化:
/**
* Sets the signature to be visible. It creates a new visible signature field.
* @param pageRect the position and dimension of the field in the page
* @param page the page to place the field. The fist page is 1
* @param fieldName the field name or <CODE>null</CODE> to generate automatically a new field name
*/
virtual public void SetVisibleSignature(Rectangle pageRect, int page, String fieldName)
我正在尝试使用 iTextSharp 5.5.9 签署 pdf 文件。自定义外观时,我收到一个无法理解的错误。这是我所做的
签名方式:
public void Sign1(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);
FileStream os = new FileStream(dest, FileMode.Create);
PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '[=10=]');
// Creating the appearance
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
appearance.Reason = reason;
appearance.Location = location;
appearance.SetVisibleSignature(name);
// Custom text and custom font
appearance.Layer2Text = "This document was signed by ABC";
appearance.Layer2Font = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN);
// Creating the signature
IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm);
MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, subfilter);
}
以及我签名的方式
String KEYSTORE = "C:/Users/user/Desktop/temp.pfx";
char[] PASSWORD = "blabla".ToCharArray();
String SRC = "C:/Users/user/abc.pdf";
String DEST = "C:/Users/user/Desktop/aaa.pdf";
Pkcs12Store store = new Pkcs12Store(new FileStream(KEYSTORE, 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;
Sign1(SRC, "Signature1", String.Format(DEST, 1), chain, parameters, DigestAlgorithms.SHA256, CryptoStandard.CMS, "Customize apprearance", "Blabla");
我收到的消息:
如何解决?
an error which is unintelligible
相反,它明确指出某些参数值不合适,附加信息甚至指出哪个参数值和原因:
The field Signature1 does not exist.
"Signature1" 是您用作签名字段名称的值,您的 Sign1
方法在此处使用它:
appearance.SetVisibleSignature(name);
您调用的方法记录在如下来源中:
/**
* Sets the signature to be visible. An empty signature field with the same name must already exist.
* @param fieldName the existing empty signature field name
*/
virtual public void SetVisibleSignature(String fieldName)
但您的 PDF 似乎还没有同名的空签名字段,这会触发您观察到的异常。
如果您想签署(使用签名的文档内可视化)PDF 但不能使用现有的空签名字段,您必须使用该方法的不同重载,您可以向该方法提供必要的信息来创建签名字段可视化:
/**
* Sets the signature to be visible. It creates a new visible signature field.
* @param pageRect the position and dimension of the field in the page
* @param page the page to place the field. The fist page is 1
* @param fieldName the field name or <CODE>null</CODE> to generate automatically a new field name
*/
virtual public void SetVisibleSignature(Rectangle pageRect, int page, String fieldName)