iText 7:旋转时生成的图章断裂

iText 7: Generated Stamp Breaks Upon Rotation

我正在尝试使用 iTextsharp 创建图章 - 就我而言,结果图章的位置和大小得到了适当的处理。但是,一旦用户使用 reader 打开文档,尝试旋转图章只会破坏它。邮票变成空白框"X",如下图:

我使用的代码如下:

Rectangle location = new Rectangle(crop.GetLeft(),crop.GetBottom(),iWidth/4,iHeight/4);

PdfStampAnnotation stamp = new PdfStampAnnotation(location).SetStampName(new PdfName("Logo"));

PdfFormXObject xObj = new PdfFormXObject(new Rectangle(iWidth, iHeight));
PdfCanvas canvas = new PdfCanvas(xObj, pdfDoc);
canvas.AddImage(img, 0, 0,iWidth, false);
stamp.SetNormalAppearance(xObj.GetPdfObject());

stamp.SetFlags(PdfAnnotation.PRINT);

pdfDoc.GetFirstPage().AddAnnotation(stamp);
pdfDoc.Close();

我很想说图像没有与矩形绑定并且最终在旋转时分开。但是,假设 邮票创建正确,这让我感到困惑,因为这是通过 Acrobat 完成的操作。我假设 reader 在尝试旋转图像之前首先旋转并调整边界矩形的大小来尝试旋转。

还值得注意的是,缩放和移动图章确实有效 - 是否有一个属性我忘记包含在我的图章构造中?

预先警告:以下是在 Windows 上仅使用 Adob​​e Acrobat Reader DC 进行反复试验的结果。结果可能因不同平台、不同版本以及不同查看器产品而异。

规格

就 PDF 规范而言,您的方法没有问题。关于邮票注释的全部内容是

12.5.6.12 Rubber Stamp Annotations

A rubber stamp annotation (PDF 1.3) displays text or graphics intended to look as if they were stamped on the page with a rubber stamp. When opened, it shall display a pop-up window containing the text of the associated note. Table 181 shows the annotation dictionary entries specific to this type of annotation.

Table 181 – Additional entries specific to a rubber stamp annotation

Subtype name (Required) The type of annotation that this dictionary describes; shall be Stamp for a rubber stamp annotation.

Name name (Optional) The name of an icon that shall be used in displaying the annotation. Conforming readers shall provide predefined icon appearances for at least the following standard names:

Approved, Experimental, NotApproved, AsIs, Expired , NotForPublicRelease, Confidential, Final, Sold, Departmental, ForComment, TopSecret, Draft, ForPublicRelease

也可能支持其他名称。默认值:草稿。

注释字典的 AP 条目(如果存在)应优先于 Name 条目;参见 Table 168 和 12.5.5,“外观流”。

您选择的名称 徽标 不在该枚举中,但毕竟您提供了自定义外观。

Adobe Reader

因此,此问题与 PDF 无关,而是与 Adob​​e Reader 实施橡皮图章注释旋转更改的方式有关。

我在这方面测试了 Adob​​e Acrobat Reader DC 的行为,事实证明,在旋转时,它总是从它自己已知的资源中重新创建橡皮图章注释的外观,每当使用该 X 图形时它不知道注释类型。例如。如果您更改由 Adob​​e Reader 创建的标准注释的外观然后旋转它,它会恢复其 Adob​​e Reader 外观。

所以它总是在旋转时重新创建橡皮图章注释的外观除非它把它识别为自定义的、用户定义的图章!它通过以“#”字符开头的名称识别用户定义的图章。

修复

因此,如果您更改

PdfStampAnnotation stamp = new PdfStampAnnotation(location).SetStampName(new PdfName("Logo"));

PdfStampAnnotation stamp = new PdfStampAnnotation(location).SetStampName(new PdfName("#Logo"));

生成的 PDF 应该按预期运行(至少在这里是这样)。

注意:由于规范未规定此行为,因此可能随时更改。

例如当我在此处使用 Adob​​e Reader 创建用户定义图章时,它的名称为 #zKzrX95V9NYDDQGyrLjmOA。这个名字中可能有校验和字母之类的东西,或者可能是外观的哈希之类的东西。在这种情况下,Adobe 可能会在某些更高版本中开始只识别橡皮图章注释,这些注释也满足用户定义的条件。

违反规范?

您可能想知道这是否违反了上面引用的没有为 "user defined annotations".

定义特殊名称的规范

不是。

规范主要定义了如何显示特定的 PDF,几乎没有指定如何编辑

特别是上面引用的处方

The annotation dictionary’s AP entry, if present, shall take precedence over the Name entry

仅指显示 PDF,不涉及编辑它们。