Itext 已定义签名。必须在PdfSignatureAppearance中关闭
我在签署pdf文件时遇到以下错误。错误是 “已定义签名。必须在PDFSignaturePearance中关闭。” 我第一次能够签署pdf文件。它在输出文件夹中创建一个pdf文件,签名在第一页。到目前为止,代码运行良好。 现在,当我将最近生成的文件作为输入在第二个页面中签名时,我得到错误“签名已定义。必须在PdfSignatureAppearance中关闭。” 我在下面的一行中得到了错误Itext 已定义签名。必须在PdfSignatureAppearance中关闭,itext,Itext,我在签署pdf文件时遇到以下错误。错误是 “已定义签名。必须在PDFSignaturePearance中关闭。” 我第一次能够签署pdf文件。它在输出文件夹中创建一个pdf文件,签名在第一页。到目前为止,代码运行良好。 现在,当我将最近生成的文件作为输入在第二个页面中签名时,我得到错误“签名已定义。必须在PdfSignatureAppearance中关闭。” 我在下面的一行中得到了错误 appearance.SetVisibleSignature(new iTextSharp.text.Rect
appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(300, 40, 530, 120), pageNo, "Icsi-Vendor");
请在下面查找代码
if (File.Exists(fName))
{
PdfReader.unethicalreading = true;
using (PdfReader pdfReader = new PdfReader(fName))
{
//file name
fName = fName.Substring(fName.LastIndexOf("\\") + 1);
outputFile = outputFolder + fName + ".pdf";
if (!File.Exists(outputFile))
{
using (FileStream fout = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite))
{
using (PdfStamper stamper = PdfStamper.CreateSignature(pdfReader, fout, '\0'))
{
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
string imagePath = txtImage.Text;
iTextSharp.text.Image signatureFieldImage = iTextSharp.text.Image.GetInstance(imagePath);
appearance.SignatureGraphic = signatureFieldImage;
signatureFieldImage.SetAbsolutePosition(250, 50);
stamper.GetOverContent(pageNo).AddImage(signatureFieldImage);
appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(300, 40, 530, 120), pageNo, "Icsi-Vendor");
appearance.Reason = txtReason.Text;
IExternalSignature es = new PrivateKeySignature(pk, "SHA-256");
MakeSignature.SignDetached(appearance, es, new X509Certificate[] { pk12.GetCertificate(alias).Certificate }, null, null, null, 0, CryptoStandard.CMS);
stamper.Close();
}
}
}
}
this.Invoke(new BarDelegate(UpdateBar), fName);
}
如果需要更多详细信息,请有人帮助我并告诉我。OP代码中有多个问题: 正确的最后一击 应用签名时,不能关闭压模对象本身,而是关闭签名外观对象。如果使用像
MakeSignature.SignDetached
这样的助手方法,甚至不必对关闭进行编码,因为SignDetached
已经隐式关闭了最后一行中的外观
因此,请
- 卸下压模。关闭()
和
PdfStamper stamper=PdfStamper.CreateSignature(pdfReader,fout,'\0')
放入中,因为这会导致调用stamper的Dispose
方法,该方法依次调用Close
MakeSignature.signDistached
中隐式出现close后,将忽略进一步的close调用
但是,如果您没有做到这一点,例如,由于以前的一些错误情况,这种关闭调用会导致您观察到的错误,在您的情况下,关闭调用是由using
指令引起的
SetVisibleSignature中的问题
您正在获取错误信息
appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(300, 40, 530, 120), pageNo, "Icsi-Vendor");
不幸的是,此行中发生的实际错误被use
指令导致的Dispose
调用期间Close
调用引起的错误所取代
考虑到信息代码:
/**
* 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) {
if (fieldName != null) {
if (fieldName.IndexOf('.') >= 0)
throw new ArgumentException(MessageLocalization.GetComposedMessage("field.names.cannot.contain.a.dot"));
AcroFields af = writer.GetAcroFields();
AcroFields.Item item = af.GetFieldItem(fieldName);
if (item != null)
throw new ArgumentException(MessageLocalization.GetComposedMessage("the.field.1.already.exists", fieldName));
this.fieldName = fieldName;
}
if (page < 1 || page > writer.reader.NumberOfPages)
throw new ArgumentException(MessageLocalization.GetComposedMessage("invalid.page.number.1", page));
this.pageRect = new Rectangle(pageRect);
this.pageRect.Normalize();
rect = new Rectangle(this.pageRect.Width, this.pageRect.Height);
this.page = page;
}
显而易见的原因是
- 包含点的字段名
- 文档中已存在命名字段,或
- 无效的页码
PdfStamper stamper = PdfStamper.CreateSignature(pdfReader, fout, '\0', true);
参见CreateSignature
方法重载注释
/**
* Applies a digital signature to a document, possibly as a new revision, making
* possible multiple signatures. The returned PdfStamper
* can be used normally as the signature is only applied when closing.
* <p>
... (outdated Java example code) ...
* @param reader the original document
* @param os the output stream or <CODE>null</CODE> to keep the document in the temporary file
* @param pdfVersion the new pdf version or '\0' to keep the same version as the original
* document
* @param tempFile location of the temporary file. If it's a directory a temporary file will be created there.
* If it's a file it will be used directly. The file will be deleted on exit unless <CODE>os</CODE> is null.
* In that case the document can be retrieved directly from the temporary file. If it's <CODE>null</CODE>
* no temporary file will be created and memory will be used
* @param append if <CODE>true</CODE> the signature and all the other content will be added as a
* new revision thus not invalidating existing signatures
* @return a <CODE>PdfStamper</CODE>
* @throws DocumentException on error
* @throws IOException on error
*/
public static PdfStamper CreateSignature(PdfReader reader, Stream os, char pdfVersion, string tempFile, bool append)
您在
stamper.Close()
过程中遇到错误,错误消息会告诉您该怎么做:不要关闭stamper
,关闭外观
。非常感谢,您的帮助非常大。您提供代码的方式非常棒。问题解决了。