java - 使用java itext或pdfbox在现有pdf中添加便笺

标签 java pdfbox

我正在尝试向现有 pdf 添加便签。请提供有关 itext 或 pdfbox 的任何建议。

我尝试过使用 pdfbox 但找不到任何解决方案。 请帮忙...

以下是我想要的便签类型的 pdf 示例:http://www.pdfill.com/example/pdf_commenting_new.pdf

最佳答案

我找到了解决方案。

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);

它可能对 future 的开发人员有用:-)

关于java - 使用java itext或pdfbox在现有pdf中添加便笺,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60374131/

相关文章:

java - 我得到一个意外的整数值

java - 不兼容的类型 : void cannot be converted to string

java - pdfbox和itext无法提取图像

java - 如何重新排序对象列表?

java - JTree 中的颜色行

java - 将一个字符与一个字符数组进行比较?

eclipse - 将PDF文件转换为HDFS(JAVA)上的文本

java - PDFbox 异常 - 线程 "main"java.lang.VerifyError 中出现异常

java - 使用 PDFBox 的表格/报告格式

java - 根据给定模板使用 Apache PDFBox 库生成自定义 pdf 布局?