添加注释后 ITextsharp 得到 "at least one signature requires validating"

ITextsharp get "at least one signature requires validating" after add annotation

我想使用 ITextsharp 将注释自由文本添加到签名的 PDF 中。将注释添加到 PDF 并在 adobe reader 中打开后,它会在 op 上显示消息“至少一个签名需要验证...”。如果我使用 adobe reader 在签名的 PDF 中添加注释,它不会显示消息。

这是我的 C# 代码并使用 itextsharp 5.5.8

using (var ms = new MemoryStream())
        {

            PdfReader reader = new PdfReader(file);
            PdfStamper stamper = new PdfStamper(reader, ms, '[=11=]', true);

            Rectangle annotRect = new Rectangle(100, 100, 150, 150);

            PdfContentByte canvas = new iTextSharp.text.pdf.PdfContentByte(stamper.Writer);

            BaseFont bf = BaseFont.CreateFont("c:\windows\fonts\mingliu.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            canvas.SetColorFill(BaseColor.RED);
            canvas.SetColorStroke(BaseColor.RED);

            PdfAnnotation annotation = PdfAnnotation.CreateFreeText(stamper.Writer, annotRect, "test", canvas);

            annotation.Flags = PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_LOCKED | PdfAnnotation.FLAGS_PRINT;


            PdfDate pdfdate = new PdfDate();
            annotation.Title = "test";
            annotation.Put(PdfName.CREATIONDATE, pdfdate);
            annotation.Put(PdfName.M, pdfdate);
            stamper.AddAnnotation(annotation, 1);

            stamper.Close();
            reader.Close();
            return ms.ToArray();
        }

问题是不是,正如我最初假设的那样,要标记的文档中存在的签名禁止添加注释。

相反,这里的问题是 Adob​​e Reader(我使用 Adob​​e Acrobat Reader DC 版本 2015.009.20079 进行了测试)仅识别注释(然后在签名后接受它们作为允许的更改)如果它们满足一些额外的条件。

通过反复试验,我发现如果我提供可选的 RC 注释字典条目,我可以让 Reader 识别您的自由文本注释。根据规范:

RC text string or text stream (Optional; PDF 1.5) A rich text string (see 12.7.3.4, “Rich Text Strings”) that shall be used to generate the appearance of the annotation.

(Table 174 – Additional entries specific to a free text annotation - in ISO 32000-1)

我是这样添加的(借用我使用 Acrobat 创建的文本注释):

annotation = PdfAnnotation.CreateFreeText(stamper.Writer, annotRect, "test", canvas);

annotation.Put(PdfName.RC, new PdfString("<?xml version=\"1.0\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"Acrobat:9.5.5\" xfa:spec=\"2.0.2\"  style=\"font-size:12.0pt;text-align:left;color:#FF0000;font-weight:normal;font-style:normal;font-family:Helvetica,sans-serif;font-stretch:normal\"><p dir=\"ltr\"><span style=\"font-family:Helvetica\">test</span></p></body>"));

没有它,Reader 会像这样显示 PDF:

... 像这样:

如您所见,Reader 仅在添加 RC 值后才在签名面板上显示注释。