简体   繁体   English

无法使itext矩形与注释一起正常使用

[英]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. 我是itext的新手,但我的注释图标无法正确显示。

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. 但是,该图标无法显示,或者只是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 : 根据JavaDocs

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

Constructs a Rectangle -object. 构造一个Rectangle

Parameters: 参数:

llx - lower left x llx-左下角x

lly - lower left y lly-左下y

urx - upper right x urx-右上方x

ury - upper right y ury-右上y

As your lower left x equals your upper right x , the rectangle has a zero width. 由于左下角x等于右上角x ,矩形的宽度为零。 Thus, it is not surprising that 因此,毫不奇怪

the icon fails to appear or is just a small dot in the PDF. 图标无法显示,或者只是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: 另一个问题:通过将注释设置为块注释并(大概)将块添加到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 . 而是使用PdfWriteraddAnnotation方法。


Furthermore you add a Popup annotation which is not related to any other annotation. 此外,您添加了一个与其他任何注释都不相关的Popup注释。 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. 弹出注释(PDF 1.3)在弹出窗口中显示文本,以供输入和编辑。 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 使用iText为父注释parent和使用以下命令的弹出注释popup构建弹出窗口-父关系

parent.setPopup(popup);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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