使用PDFBox,如何在注释上设置外观流?

时间:2014-11-18 12:48:44

标签: java image annotations pdfbox

我正在尝试突出显示某些文字并将其转换为图片。 我尝试了一些东西但是图片上没有出现注释。 寻求帮助发现了这个问题 http://issues.apache.org/jira/browse/PDFBOX-2162 其中说我必须设置外观流注释,这是acrobat阅读器自动完成的,但是当转换为图像时需要它。我无法弄清楚如何将外观流设置为注释。 寻找注释和外观流的一些例子 (我找不到两个例子...... :-()

这是我到目前为止所做的:

PDDocument document = PDDocument.load("sometest.pdf");
List<PDPage> pages = document.getDocumentCatalog().getAllPages();
PDPage page = pages.get(0);
List<PDAnnotation> annotations = page.getAnnotations();
PDAnnotationTextMarkup annotation = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);

final PDRectangle boundingBox = new PDRectangle();
//here I set the boundingbox coordinates
annotation.setRectangle(boundingBox);

final float[] quads = this.getQuads(boundingBox);
annotation.setQuadPoints(quads);
annotation.setContents("bla bla");
annotation.setConstantOpacity((float) 0.9);
PDGamma c = new PDGamma();
//Here I set the RGB
annotation.setColour(c);
annotation.setPrinted(true);
//create the Form for the appearance stream
PDResources holderFormResources = new PDResources();
PDStream holderFormStream = new PDStream(document);
PDXObjectForm holderForm = new PDXObjectForm(holderFormStream);
holderForm.setResources(holderFormResources);
holderForm.setBBox(boundingBox);
holderForm.setFormType(1);
// trying to set the appreanceStream for the annotation
PDAppearanceDictionary appearance = new PDAppearanceDictionary();
appearance.getCOSObject().setDirect(true);
PDAppearanceStream appearanceStream = new PDAppearanceStream(holderForm.getCOSStream());
holderForm.getCOSStream().createFilteredStream();
appearance.setNormalAppearance(appearanceStream);

annotation.setAppearance(appearance);

annotations.add(annotation);
//convert to image
BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_BGR, 600);
ImageIO.write(image, "jpg", new File("test.jpg"));
document.save("test.pdf");
document.close();

“test.pdf”带有注释,但图像没有。

我错过了什么?

0 个答案:

没有答案