【问题标题】:Error adding second Digital Signature to PDF in "PDFsharp"在“PDFsharp”中向 PDF 添加第二个数字签名时出错
【发布时间】:2019-02-07 06:36:30
【问题描述】:

我已使用 PDFsharp 库中的特定 pull request 使用 PDFsharp 向 PDF 添加数字签名。如果它是一个未签名的文件,它工作得很好。

当我尝试向已签名的 PDF 添加第二个签名时,收到错误“/sigflags”已存在。
是否可以附加到“/ sigflags”?而不是尝试添加它?

 private void AddSignatureComponents(object sender, EventArgs e)
 {
      var catalog = Document.Catalog;
      if (catalog.AcroForm == null)
         catalog.AcroForm = new PdfAcroForm(Document);
         catalog.AcroForm.Elements.Add(PdfAcroForm.Keys.SigFlags, new PdfInteger(3));

         var signature = new PdfSignatureField(Document);
         var paddedContents = new PdfString("", PdfStringFlags.HexLiteral, maximumSignatureLength.Value);
         var paddedRange = new PdfArray(Document, byteRangePaddingLength, new PdfInteger(0), new PdfInteger(0), new PdfInteger(0), new PdfInteger(0));
         signature.Contents = paddedContents;
         signature.ByteRange = paddedRange;
         signature.Reason = Options.Reason;
         signature.Location = Options.Location;
         signature.Rectangle = new PdfRectangle(Options.Rectangle);
         signature.AppearanceHandler = Options.AppearanceHandler ?? new DefaultAppearanceHandler()
         {
                Location = Options.Location,
                Reason = Options.Reason,
                Signer = Certificate.GetNameInfo(X509NameType.SimpleName, false)
         };
         signature.PrepareForSave();
         this.contentsTraker = new PositionTracker(paddedContents);
         this.rangeTracker = new PositionTracker(paddedRange);
         foreach (var pagenumber in Options.PageNumber)
         {
            var index = pagenumber - 1;

            if (!Document.Pages[index].Elements.ContainsKey(PdfPage.Keys.Annots))
                Document.Pages[index].Elements.Add(PdfPage.Keys.Annots, new PdfArray(Document));
                try
                {
                    (Document.Pages[index].Elements[PdfPage.Keys.Annots] as PdfArray).Elements.Add(signature);
                }
                catch
                {
                    if (Document.Pages[index].Elements.ContainsKey(PdfPage.Keys.Annots))
                    {
                        Document.Pages[index].Elements.Remove(PdfPage.Keys.Annots);
                        Document.Pages[index].Elements.Add(PdfPage.Keys.Annots, new PdfArray(Document));
                    }
                    (Document.Pages[index].Elements[PdfPage.Keys.Annots] as PdfArray).Elements.Add(signature);
                }
            }
            catalog.AcroForm.Fields.Elements.Add(signature);
    }

【问题讨论】:

  • 这个有什么解决办法吗?
  • 还没有解决办法
  • Document.Catalog 中 Document 的类型是什么?

标签: c# pdfsharp


【解决方案1】:

AcroForm 中的 SigFlags 条目只应出现一次,并且仅在存在签名字段或数字签名时出现。这就是您收到第二个签名错误的原因。如果存在签名字段,则 PdfInteger 值位 0 仅应为 1,如果存在数字签名,则位 1 应仅为 1。通过将其设置为 3,您表示(在您的情况下正确)存在签名字段和数字签名。 您在代码中需要做的就是检查 SigFlags 条目是否存在于 AcroForm 中,如果它被覆盖,否则添加它。

if (catalog.AcroForm == null)
   { catalog.AcroForm = new PdfAcroForm(Document);}

if (catalog.AcroForm.Elements.ContainsKey(PdfAcroForm.Keys.SigFlags))     
  {  catalog.AcroForm.Elements[PdfAcroForm.Keys.SigFlags] = new PdfInteger(3);}
else
   { catalog.AcroForm.Elements.Add(PdfAcroForm.Keys.SigFlags, new PdfInteger(3));}

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2013-01-04
    • 2019-09-30
    相关资源
    最近更新 更多