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

Java 使用PDFBox,如何在注释上设置外观流?,java,image,annotations,pdfbox,Java,Image,Annotations,Pdfbox,我试图突出显示一些文本并将其转换为图像。 我尝试了一些东西,但是图像上没有注释。 寻求帮助时发现此问题 这意味着我必须将外观流设置为注释,AcrobatReader会自动执行这一操作,但在转换为图像时需要它。我不知道如何将外观流设置为注释。 查找有关注释和外观流的一些示例 (我找不到一个能同时做到这两个方面的例子:-() 这就是我到目前为止所做的: PDDocument document = PDDocument.load("sometest.pdf"); List<PDPage>

我试图突出显示一些文本并将其转换为图像。 我尝试了一些东西,但是图像上没有注释。 寻求帮助时发现此问题 这意味着我必须将外观流设置为注释,AcrobatReader会自动执行这一操作,但在转换为图像时需要它。我不知道如何将外观流设置为注释。 查找有关注释和外观流的一些示例 (我找不到一个能同时做到这两个方面的例子:-()

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

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();
PDDocument document=PDDocument.load(“sometest.pdf”);
列表页面=document.getDocumentCatalog().getAllPages();
PDPage=pages.get(0);
列表注释=page.getAnnotations();
PDAnnotationTextMarkup annotation=新的PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
最终PDRectangle boundingBox=新PDRectangle();
//这里我设置了边界框坐标
注释.setRectangle(边框);
final float[]quads=this.getQuads(boundingBox);
注释:设定点(四边形);
注释.setContents(“bla-bla”);
注释:SetConstantTopacity((浮点)0.9);
PDGamma c=新的PDGamma();
//这里我设置了RGB
注释.颜色(c);
注释.setPrinted(true);
//为外观流创建表单
PDResources holderFormResources=新的PDResources();
PDStream holderFormStream=新的PDStream(文档);
PDXObjectForm HoldPerform=新的PDXObjectForm(HoldPerformStream);
HoldPerform.setResources(HoldPerformResources);
HoldPerform.SetBox(边界框);
HoldPerform.setFormType(1);
//正在尝试设置批注的appreanceStream
PDAppearanceDictionary外观=新的PDAppearanceDictionary();
外观.getCOSObject().setDirect(true);
PDAppearanceStream appearanceStream=新的PDAppearanceStream(holderForm.getcostream());
HoldPerform.GetCoStream().createFilteredStream();
外观.setNormalAppearance(外观流);
注释.设置外观(外观);
注释。添加(注释);
//转换为图像
BuffereImage image=page.convertToImage(buffereImage.TYPE_INT_BGR,600);
write(图像,“jpg”,新文件(“test.jpg”);
文件保存(“test.pdf”);
document.close();
“test.pdf”带有注释,但图像没有


我遗漏了什么?

如果Adobe Reader的PDF显示良好,但渲染的图像看起来不正常,那么您应该在JIRA中打开一个问题并附加该PDF。至少有一个错误涉及到未发布的2.0版本的注释渲染(PDFBOX-2283),但1.8版本的注释渲染(请参阅PDFBOX-2001中的补丁)。您的代码创建了一个空的外观流,PDFBox会将其忠实地呈现到JPEG中。您会在PDF查看器中看到一些东西,因为PDF查看器会感知到空的外观,从而创建其选择的外观。好的,那么如何创建一个包含将呈现到JPEG中的内容的外观流呢?很遗憾,现在已经澄清了什么是空的你的意思是,这是一个已知的bug,也在PDF.js中,请参阅PDFBOX-2019。我也有类似的问题,@chico,你找到解决方案了吗?如果是,那将非常有用。