简体   繁体   中英

Can't get itext Rectangle to work correctly with annotations

I'm new to itext and I can't get my annotation icons to appear correctly.

I'm trying to create a rectangle for my annotation icon. My example is below:

Rectangle rect = new Rectangle(164, 190, 164, 110);

chunk_text.setAnnotation(PdfAnnotation.createText(writer, rect, "Warning", comments, false, "Comment"));        

PdfContentByte pdb = new PdfContentByte(writer);

chunk_free.setAnnotation(PdfAnnotation.createFreeText(writer, rect, comments, pdb));

chunk_popup.setAnnotation(PdfAnnotation.createPopup(writer, rect, comments, false));

But, the icon fails to appear or is just a small dot in the PDF.

I cant find what Im doing wrong.

You create the rectangle like this

Rectangle rect = new Rectangle(164, 190, 164, 110);

According to the JavaDocs :

  public Rectangle(float llx, float lly, float urx, float ury) 

Constructs a Rectangle -object.

Parameters:

llx - lower left x

lly - lower left y

urx - upper right x

ury - upper right y

As your lower left x equals your upper right x , the rectangle has a zero width. Thus, it is not surprising that

the icon fails to appear or is just a small dot in the PDF.

Thus, use coordinates describing a large enough rectangle, eg

Rectangle rect = new Rectangle(164, 190, 328, 300);

Another issue: You add the annotation by setting it as a chunk annotation and (presumably) adding that chunk to the PDF:

chunk_text.setAnnotation(PdfAnnotation.createText(writer, rect, "Warning", comments, false, "Comment"));        

This technique eventually anyways replaces your rectangle by the bounding box of the rendered chunk text. Thus, it might not really be what you want. Instead use the addAnnotation method of your PdfWriter .


Furthermore you add a Popup annotation which is not related to any other annotation. This does not make sense, according to the specification:

A pop-up annotation (PDF 1.3) displays text in a pop-up window for entry and editing. It shall not appear alone but is associated with a markup annotation, its parent annotation, and shall be used for editing the parent's text.

Using iText to build that popup-parent relation for a parent annotation parent and a popup annotation popup using

parent.setPopup(popup);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM