本文介绍了iText 7:旋转生成的图章时会中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iTextsharp创建图章-就我而言,所得到图章的位置和大小已得到适当处理.但是,一旦用户使用阅读器打开文档,尝试旋转图章就会将其破坏.印章变成带有"X"的空白框,如下所示:

I'm trying to create stamps using iTextsharp - as far as I'm concerned, the position and size of the resulting stamp is handled appropriately. However, once the user opens the document using the reader, attempting to rotate the stamp simply breaks it. The stamp turns into blank box with an "X" as shown below:

我正在使用的代码如下:

The code I am using is as follows:

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进行的操作.我假设读者在尝试旋转图像之前先旋转并调整边界矩形的大小来尝试旋转.

I am tempted to say that the image is not bound with the rectangle and ends up breaking apart upon rotation. However, assuming the stamp is created correctly, it seems confusing to me because this is a manipulation done via Acrobat. I would assume that the reader attempts rotation by first rotating and resizing the bounding rectangle before trying to rotate the image.

值得注意的是,缩放和移动图章确实可以工作-我忘记在图章的构造中包含一个属性吗?

It is also worth noting that scaling and moving the stamp DOES work - is there an attribute I forgot to include in my construction of the stamp?

推荐答案

前面警告:以下是在Windows上仅使用Adobe Acrobat Reader DC反复试验的结果.在不同的平台,不同的版本以及最可能的不同查看器产品上,结果可能会有所不同.

Up front a warning: The following is a result of trial and error using only Adobe Acrobat Reader DC on Windows. Results might differ on different platforms, for different versions, and most probably for different viewer products.

就PDF规范而言,没有问题.关于图章注释的全部内容是

As far as the PDF specification is concerned, there is nothing wrong with your approach. All it says about stamp annotations is

表181 –特定于橡皮戳注释的其他条目

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:

也可以支持其他名称.默认值:草稿.

Additional names may be supported as well. Default value: Draft.

注释词典的 AP 条目(如果存在)应优先于 Name 条目;请参见表168和12.5.5外观流".

The annotation dictionary’s AP entry, if present, shall take precedence over the Name entry; see Table 168 and 12.5.5, "Appearance Streams."

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

Your chosen name Logo is not in that enumeration but you supply a custom appearance after all.

因此,此问题与PDF无关,而是与Adobe Reader实现橡皮图章"注释旋转更改的方式有关.

Thus, this issue has nothing to do with the PDF but with the way Adobe Reader implements Rubber Stamp annotation rotation changes.

我在这方面测试了Adobe Acrobat Reader DC的行为,结果发现,旋转时,它总是从自身已知的资源中重新创建橡皮图章注释的外观,只要不知道该注释,便使用该X图形类型.例如.如果您更改由Adobe Reader创建的标准注释的外观,然后旋转它,则它会恢复其Adobe Reader的外观.

I tested the behavior of Adobe Acrobat Reader DC in this regard and it turns out that upon rotation it always recreates the appearance of a rubber stamp annotation from the resources known to itself, using that X graphic whenever it does not know the annotation type. E.g. if you change the appearance of a standard annotation created by Adobe Reader and then rotate it, it regains its Adobe Reader look and feel.

因此,它始终会在旋转时重新创建橡皮图章注释的外观 ,除非它将其识别为用户定义的自定义图章! ,并且它会根据用户识别的图章来识别名称以#"字符开头.

So it always recreates the appearance of a rubber stamp annotation upon rotation unless it recognizes it as a custom, user defined stamp! And it recognizes user defined stamps by their name starting with a '#' character.

因此,如果您进行更改

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

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

生成的PDF应该表现出预期的效果(至少在这里如此).

the resulting PDF should behave as desired (at least it does so here).

当心:由于此行为不是规范所规定的,因此可能随时更改.

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

E.g. when I created a user defined stamp with Adobe Reader here, it got the name #zKzrX95V9NYDDQGyrLjmOA. There might be something like a checksum letter in this name, or it might be something like a hash of the appearance or whatever. In such a case Adobe might start in some later version to only recognize rubber stamp annotations which also fulfill this condition as user defined.

您可能想知道这是否违反了上面引用的规范,该规范没有为用户定义的注释"定义特殊名称.

You might wonder whether this is a violation of the specification as quoted above which does not define special names for "user defined annotations".

不是.

该规范主要定义了如何显示 特定的PDF,几乎没有指定如何对其进行编辑.

The specification mostly defines how a specific PDF is to be displayed, it hardly specifies how it is to be edited.

尤其是上面引言中的处方

In particular the prescription in the quote above

仅表示显示PDF,而不是对其进行编辑.

only refers to displaying PDFs, not to editing them.

这篇关于iText 7:旋转生成的图章时会中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 12:01