我正在使用iText7在文档中添加多个签名字段。 我的情况如下:
在文档中添加1个签名字段并签名。该签名已在Adobe中验证。 在同一文档中添加第二签名字段,然后再次对文档签名。在Adobe中打开文档后,将验证最后一个签名,但在已签名的签名字段上显示以下错误:
签名包含不正确,无法识别,损坏或可疑的数据。支持信息:SigDict /内容非法数据
用于将签名字段添加到PDF的代码段如下:
using (Stream memoryStream = new MemoryStream())
using (PdfReader _pdfReader = new PdfReader(new
MemoryStream(_documentBytes)).SetUnethicalReading(true))
using (PdfDocument pdfDocument = new PdfDocument(_pdfReader, new PdfWriter(memoryStream)))
{
PdfAcroForm pdfAcroForm = PdfAcroForm.GetAcroForm(pdfDocument, true);
foreach (PDFSignatureField field in signatureFields)
{
//Initializing signature position object
PDFSignaturePosition SigPosition = field.Position;
for (int i = 0; i < SigPosition.Pages.Length; ++i)
{
//Getting PDF document page
PdfPage page = pdfDocument.GetPage(SigPosition.Pages[i]);
if (page == null)
{
page = pdfDocument.GetPage(SigPosition.PageNumber);
}
//Getting PDF document page rotation
int rotation = page.GetRotation();
//Getting signature field rects according to PDF page
iText.Kernel.Geom.Rectangle rect;
if (rotation > 0 && SigPosition.Position == PDFSignaturePosition.DefaultSignaturePosition.Custom)
{
rect = GetSignaturePositionAccordingToRotation(SigPosition.Rect, page.GetCropBox(), rotation);
}
else
{
rect = GetSignaturePositionRect(SigPosition.Position, SigPosition.Rect, page.GetCropBox());
}
//Creating signature field into PDF page
PdfFormField sig = PdfSignatureFormField.CreateSignature(pdfDocument, rect);
//PdfFormField.CreateSignature(pdfDocument, rect);
//Setting signature field visible flag
if (field.Display == (int)SignatureDisplayType.INVISIBLE)
{
sig.SetFieldFlags(PdfFormField.HIDDEN);
sig.SetVisibility(PdfFormField.HIDDEN);
}
else
{
sig.SetFieldFlags(PdfFormField.VISIBLE);
sig.SetVisibility(PdfFormField.VISIBLE);
}
//Setting signature field name
sig.SetFieldName(i == 0 ? field.Name : field.Name + " - " + SigPosition.Pages[i]);
//Setting signature field page
sig.SetPage(SigPosition.Pages[i]);
//Adding signature field into AcroForm
pdfAcroForm.AddField(sig);
pdfAcroForm.AddFieldAppearanceToPage(sig, page);
pdfAcroForm.SetDefaultAppearance("/Helv 0 Tf 0 g");
//Making indirect reference of the added signature field into PDF
sig.MakeIndirect(pdfDocument);
}
//Closing PDF document object
pdfDocument.Close();
//Getting latest document bytes after adding PDF field
documentBytes = ((MemoryStream)memoryStream).ToArray();
_documentBytes = documentBytes;
}
}
有人可以帮我解决这个问题。
致谢
答案 0 :(得分:1)
使用append
模式使iText创建增量更新,而不是从头开始创建新文档。即使用
PdfDocument pdfDocument = new PdfDocument(_pdfReader, new PdfWriter(memoryStream), new StampingProperties().UseAppendMode())