pdfboxを使用してPDFに付箋を追加します

ディーパック・シン:

私はpdfboxを使用して、既存のPDFに付箋を追加しようとしています。


PDRectangle position = new PDRectangle();
System.out.println(textPosition.getXDirAdj());
position.setUpperRightX(textPosition.getX());
position.setUpperRightY(ph - textPosition.getY());

PDAnnotationTextMarkup polygonMarkup = new PDAnnotationTextMarkup(PDAnnotationMarkup.SUB_TYPE_POLYGON);
polygonMarkup.setContents("text");
polygonMarkup.setRectangle(position);
polygonMarkup.setLocked(true);
polygonMarkup.setReadOnly(true);

annotations.add(polygonMarkup);
page.setAnnotations(annotations);

このコードは正常に動作しているが、それは私の最大の関心事である付箋紙を作成しません。

任意のリードが高く評価されています。

ディーパック・シン:

@mklで引用したよう

According to the PDF specification, 'a text annotation represents a “sticky note” attached to a point in the PDF document.' Thus, neither the class PDAnnotationTextMarkup nor the subtype SUB_TYPE_POLYGON appears to match your requirements. Instead, you should use the PDAnnotationText class. As an aside, PDAnnotationTextMarkup is documented (JavaDocs) to be the abstract class that represents a text markup annotation. While it is not actually declared abstract, that characterization should make clear that it probably does not work without further ado.

私は、コードの下に使用し、それは私のために魔法のように働きました

PDRectangle position = new PDRectangle();
position.setUpperRightX(textPosition.getX());
position.setUpperRightY(ph - textPosition.getY());

position.setLowerLeftX(textPosition.getX()-4);
position.setLowerLeftY(ph - textPosition.getY());
PDGamma colourBlue = new PDGamma();
colourBlue.setB(1);

PDAnnotationText text = new PDAnnotationText();
text.setContents(commentNameWithComments.get(word));
text.setRectangle(position);
text.setOpen(true);
text.setConstantOpacity(50f);

assert annotations != null;
annotations.add(text);
page1.setAnnotations(annotations);
replaceText(word);

それは、将来の開発者のための役に立つかもしれません:-)

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=5100&siteId=1