在PDFBox中添加丰富的文本注释的正确方法是什么?
我将三个注释添加到空的pdf中:
- call
.setContents(“ ...”)
- 呼叫
.setRichContents(“ ...”);
- call call
.setContents( “ ...”);
和.setRichContents(“ ...”);
第一个注释在Adobe Reader和Preview(在Mac)中正确显示。
仅在Adobe读取器(作为格式的文本)中正确显示第二个注释,而预览中的空盒子。
第三个注释显示为纯文本测试案例3
的简单文本内容 .setContent
在Adobe读取器中(不是从方法.setrichcontents 和PDF TAG
/rc
!)和预览。
在PDF中进行评论的文字包含丰富的格式化元素,我需要在注释中显示它们。我认为预览不支持注释中的丰富文本。
我试图在Adobe阅读器中重新保存PDF,然后在Preview中打开 - 此后,我在Adobe Reader(带有丰富的文本)和预览中看到了所有注释(不格式化的文本)。
问题:如何在Adobe阅读器和纯文本中显示丰富的格式化注释?
我的代码:
PDDocument document = new PDDocument();
PDPage blankPage = new PDPage();
document.addPage(blankPage);
List<PDAnnotation> annotations = document.getPage(0).getAnnotations();
float pageHeight = document.getPage(0).getCropBox().getHeight();
PDAnnotationText text_TC1 = new PDAnnotationText();
PDRectangle positionTC1 = new PDRectangle();
positionTC1.setLowerLeftX(10);
positionTC1.setLowerLeftY(pageHeight - 10);
positionTC1.setUpperRightX(20);
positionTC1.setUpperRightY(pageHeight - 20);
text_TC1.setContents("Simple text content for Test Case 1.");
text_TC1.setRectangle(positionTC1);
text_TC1.setOpen(true);
text_TC1.constructAppearances();
annotations.add(text_TC1);
PDAnnotationText text_TC2 = new PDAnnotationText();
PDRectangle positionTC2 = new PDRectangle();
positionTC2.setLowerLeftX(10);
positionTC2.setLowerLeftY(pageHeight - 110);
positionTC2.setUpperRightX(20);
positionTC2.setUpperRightY(pageHeight - 120);
text_TC2.setRichContents("<body xmlns=\"http://www.w3.org/1999/xhtml\">" +
"<p><span style=\"font-weight:bold\">Rich</span> <span style=\"font-style:italic\">text content</span> for Test Case 2.</p>" +
"</body>");
text_TC2.setRectangle(positionTC2);
text_TC2.setOpen(true);
text_TC2.constructAppearances();
annotations.add(text_TC2);
PDAnnotationText text_TC3 = new PDAnnotationText();
PDRectangle positionTC3 = new PDRectangle();
positionTC3.setLowerLeftX(10);
positionTC3.setLowerLeftY(pageHeight - 210);
positionTC3.setUpperRightX(20);
positionTC3.setUpperRightY(pageHeight - 220);
text_TC3.setContents("Simple text content for Test Case 3.");
text_TC3.setRichContents("<body xmlns=\"http://www.w3.org/1999/xhtml\">" +
"<p><span style=\"font-weight:bold\">Rich</span> <span style=\"font-style:italic\">text content</span> for Test Case 3.</p>" +
"</body>");
text_TC3.setRectangle(positionTC3);
text_TC3.setOpen(true);
text_TC3.constructAppearances();
annotations.add(text_TC3);
document.save("test_so.pdf");
document.close();
更新2022-05-08 :我找到了解决方案 - 通过导入XFDF添加注释:
PDDocument document = new PDDocument();
PDPage blankPage = new PDPage();
document.addPage(blankPage);
FDFDocument fdfDoc = FDFDocument.loadXFDF("test.xfdf");
List<FDFAnnotation> fdfAnnots = fdfDoc.getCatalog().getFDF().getAnnotations();
List<PDAnnotation> pdfannots = new ArrayList<>();
for (int i=0; i<fdfDoc.getCatalog().getFDF().getAnnotations().size(); i++) {
FDFAnnotation fdfannot = fdfAnnots.get(i);
PDAnnotation pdfannot = PDAnnotation.createAnnotation(fdfannot.getCOSObject());
pdfannots.add(pdfannot);
}
document.getPage(0).setAnnotations(pdfannots);
fdfDoc.close();
document.save("test.pdf");
document.close();
I add three annotations into empty PDF:
- call
.setContents("...")
- call
.setRichContents("...");
- call
.setContents("...");
and.setRichContents("...");
First annotation displays properly in Adobe Reader and in Preview (on Mac).
Second annotation displays properly in Adobe Reader only (as formatted text), and empty box in Preview.
Third annotation displays as plain text Simple text content for Test Case 3
from the method .setContent
in Adobe Reader ( NOT rich text from the method .setRichContents
and PDF tag /RC
!) and in Preview.
Text for comments in PDF contains rich formatted elements, I need to show them in annotations. I assume that Preview doesn't support rich text in annotations.
I've tried to re-save PDF in Adobe Reader and then open in Preview - after that I see all comments in Adobe Reader (with rich text) and in Preview (as not formatted text).
Question: how to show rich formatted annotation in Adobe Reader and plain text in Preview?
My code:
PDDocument document = new PDDocument();
PDPage blankPage = new PDPage();
document.addPage(blankPage);
List<PDAnnotation> annotations = document.getPage(0).getAnnotations();
float pageHeight = document.getPage(0).getCropBox().getHeight();
PDAnnotationText text_TC1 = new PDAnnotationText();
PDRectangle positionTC1 = new PDRectangle();
positionTC1.setLowerLeftX(10);
positionTC1.setLowerLeftY(pageHeight - 10);
positionTC1.setUpperRightX(20);
positionTC1.setUpperRightY(pageHeight - 20);
text_TC1.setContents("Simple text content for Test Case 1.");
text_TC1.setRectangle(positionTC1);
text_TC1.setOpen(true);
text_TC1.constructAppearances();
annotations.add(text_TC1);
PDAnnotationText text_TC2 = new PDAnnotationText();
PDRectangle positionTC2 = new PDRectangle();
positionTC2.setLowerLeftX(10);
positionTC2.setLowerLeftY(pageHeight - 110);
positionTC2.setUpperRightX(20);
positionTC2.setUpperRightY(pageHeight - 120);
text_TC2.setRichContents("<body xmlns=\"http://www.w3.org/1999/xhtml\">" +
"<p><span style=\"font-weight:bold\">Rich</span> <span style=\"font-style:italic\">text content</span> for Test Case 2.</p>" +
"</body>");
text_TC2.setRectangle(positionTC2);
text_TC2.setOpen(true);
text_TC2.constructAppearances();
annotations.add(text_TC2);
PDAnnotationText text_TC3 = new PDAnnotationText();
PDRectangle positionTC3 = new PDRectangle();
positionTC3.setLowerLeftX(10);
positionTC3.setLowerLeftY(pageHeight - 210);
positionTC3.setUpperRightX(20);
positionTC3.setUpperRightY(pageHeight - 220);
text_TC3.setContents("Simple text content for Test Case 3.");
text_TC3.setRichContents("<body xmlns=\"http://www.w3.org/1999/xhtml\">" +
"<p><span style=\"font-weight:bold\">Rich</span> <span style=\"font-style:italic\">text content</span> for Test Case 3.</p>" +
"</body>");
text_TC3.setRectangle(positionTC3);
text_TC3.setOpen(true);
text_TC3.constructAppearances();
annotations.add(text_TC3);
document.save("test_so.pdf");
document.close();
Update 2022-05-08: I've found a workaround solution - add comments via import XFDF:
PDDocument document = new PDDocument();
PDPage blankPage = new PDPage();
document.addPage(blankPage);
FDFDocument fdfDoc = FDFDocument.loadXFDF("test.xfdf");
List<FDFAnnotation> fdfAnnots = fdfDoc.getCatalog().getFDF().getAnnotations();
List<PDAnnotation> pdfannots = new ArrayList<>();
for (int i=0; i<fdfDoc.getCatalog().getFDF().getAnnotations().size(); i++) {
FDFAnnotation fdfannot = fdfAnnots.get(i);
PDAnnotation pdfannot = PDAnnotation.createAnnotation(fdfannot.getCOSObject());
pdfannots.add(pdfannot);
}
document.getPage(0).setAnnotations(pdfannots);
fdfDoc.close();
document.save("test.pdf");
document.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论