我是 itext 新手,无法正确显示注释图标。
我正在尝试为我的注释图标创建一个矩形。我的例子如下:
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));
但是,图标无法显示或只是 PDF 中的一个小点。
我找不到我做错了什么。
最佳答案
您可以像这样创建矩形
Rectangle rect = new Rectangle(164, 190, 164, 110);
根据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
由于左下角 x 等于右上角 x,因此矩形的宽度为零。因此,这并不奇怪
the icon fails to appear or is just a small dot in the PDF.
因此,使用描述足够大的矩形的坐标,例如
Rectangle rect = new Rectangle(164, 190, 328, 300);
<小时/>
另一个问题:您可以通过将其设置为 block 注释并(大概)将该 block 添加到 PDF 来添加注释:
chunk_text.setAnnotation(PdfAnnotation.createText(writer, rect, "Warning", comments, false, "Comment"));
无论如何,这种技术最终都会用渲染 block 文本的边界框替换矩形。因此,它可能并不是您真正想要的。而是使用 PdfWriter
的 addAnnotation
方法。
此外,您还添加了一个与任何其他注释都不相关的弹出注释。根据规范,这是没有意义的:
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.
使用 iText 为父注释 parent
和弹出注释 popup
构建弹出父关系
parent.setPopup(popup);
关于java - 无法让 itext 矩形与注释一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46204693/