gpt4 book ai didi

itext - 已定义签名。必须在 PdfSignatureAppearance 中关闭

转载 作者:行者123 更新时间:2023-12-01 09:55:23 25 4
gpt4 key购买 nike

签署 pdf 时出现以下错误。错误是“签名定义。必须在 PdfSignatureAppearance 中关闭。”

我可以第一次签署 pdf。它在输出文件夹中创建一个 pdf 文件,并在第一页带有签名。到目前为止,代码工作正常。现在,当我将最近生成的文件作为输入以登录第二页时,我收到错误“签名已定义。必须在 PdfSignatureAppearance 中关闭。”

我收到下面一行的错误

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 已经隐式关闭了它的 appearance最后一行。

所以请

  • 删除 stamper.Close()
  • 不要将 PdfStamper stamper = PdfStamper.CreateSignature(pdfReader, fout, '\0') 放入 using 指令中,因为这会导致调用压模的 Dispose 方法依次调用 Close

通常你不会受到这些行的伤害,因为在 MakeSignature.SignDetached 中隐式出现关闭后,进一步的关闭调用将被忽略。

但是,如果您没有做到这一点,例如由于之前的一些错误情况,这样的关闭调用会导致您观察到的错误,在您的情况下是由 using 指令引起的关闭调用。

SetVisibleSignature 中的问题

你得到了错误

appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(300, 40, 530, 120), pageNo, "Icsi-Vendor");

不幸的是,这一行中发生的实际错误被 Dispose 调用期间由于 using 导致的 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;
}

明显的原因是

  • 包含点的字段名称,
  • 文档中已存在的命名字段,或
  • 页码无效。

正如你描述你的情况

I am able to sign the pdf for the first time. It creates a pdf file in output folder with the signature in the first page. So far the code works fine. Now when I give the recently generated file as input to sign in a second page I get the error

我认为第二项是最可能的原因:如果您想在同一个文档中添加多个签名,它们的字段名称必须不同。

追加模式

当您表明您对同一个文件应用多个签名时,您必须使用附加模式。如果您不这样做,您将使之前的签名无效:

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)

关于itext - 已定义签名。必须在 PdfSignatureAppearance 中关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29115690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com