问题
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();
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?
回答1:
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.
The specification
As far as the PDF specification is concerned, there is nothing wrong with your approach. All it says about stamp annotations is
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
Additional names may be supported as well. Default value: Draft.
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.
Adobe Reader
Thus, this issue has nothing to do with the PDF but with the way Adobe Reader implements Rubber Stamp annotation rotation changes.
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.
The fix
Thus, if you change
PdfStampAnnotation stamp = new PdfStampAnnotation(location).SetStampName(new PdfName("Logo"));
to
PdfStampAnnotation stamp = new PdfStampAnnotation(location).SetStampName(new PdfName("#Logo"));
the resulting PDF should behave as desired (at least it does so here).
Beware: As this behavior is not prescribed by the specification, it might change any time.
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.
Violation of the specification?
You might wonder whether this is a violation of the specification as quoted above which does not define special names for "user defined annotations".
It is not.
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
The annotation dictionary’s AP entry, if present, shall take precedence over the Name entry
only refers to displaying PDFs, not to editing them.
来源:https://stackoverflow.com/questions/43052760/itext-7-generated-stamp-breaks-upon-rotation