如何在PDAnnotationLink中设置图片?附对应代码示例
在PDAnnotationLink中设置图片的实现方法
原代码片段(添加PDAnnotationLink)
final PDAnnotationLink txtLink = new PDAnnotationLink(); PDBorderStyleDictionary pdBorderStyle = new PDBorderStyleDictionary(); pdBorderStyle.setStyle(PDBorderStyleDictionary.STYLE_BEVELED); pdBorderStyle.setWidth(0); txtLink.setBorderStyle(pdBorderStyle); final PDActionURI actionURI = new PDActionURI(); actionURI.setURI ("https://stackoverflow.com/"); txtLink.setAction(actionURI); PDRectangle pdRectangle = new PDRectangle(); pdRectangle.setLowerLeftX(250); pdRectangle.setLowerLeftY(550); pdRectangle.setUpperRightX(400); pdRectangle.setUpperRightY(600); txtLink.setRectangle(pdRectangle); pdPage.getAnnotations().add(txtLink);
问题
上述代码运行正常,但我希望在PDAnnotationLink中设置图片,请问是否可以实现这一需求?若可以,该如何操作?
可以实现,核心是将图片作为PDF表单XObject嵌入,再通过链接注解的外观字典关联该图片,具体步骤如下:
1. 加载图片并转换为PDFormXObject
先把目标图片转为PDImageXObject,再包装成PDFormXObject,确保图片能作为注解外观使用:
// 加载本地图片(支持PNG、JPG等格式) PDImageXObject image = PDImageXObject.createFromFile("path/to/your/image.png", document); // 创建表单XObject,尺寸与图片一致 PDFormXObject form = new PDFormXObject(document); form.setResources(new PDResources()); form.setBBox(new PDRectangle(image.getWidth(), image.getHeight())); // 在表单上绘制图片 try (PDPageContentStream contentStream = new PDPageContentStream(document, form)) { contentStream.drawImage(image, 0, 0, image.getWidth(), image.getHeight()); }
2. 为PDAnnotationLink设置外观字典
创建外观字典,将表单XObject设为注解的默认外观,同时调整链接矩形尺寸匹配图片:
// 调整链接矩形的位置和尺寸(左下角坐标+宽高) PDRectangle linkRect = new PDRectangle(250, 550, image.getWidth(), image.getHeight()); txtLink.setRectangle(linkRect); // 配置外观字典 PDAppearanceDictionary appearance = new PDAppearanceDictionary(); PDAppearanceEntry appearanceEntry = new PDAppearanceEntry(); appearanceEntry.setNormalAppearance(form); appearance.setNormalAppearance(appearanceEntry); txtLink.setAppearance(appearance); // 隐藏默认边框(可选) txtLink.setBorder(new float[]{0, 0, 0});
3. 完整整合代码
将上述步骤整合到原代码中,最终实现带图片的链接注解:
final PDAnnotationLink txtLink = new PDAnnotationLink(); // 加载并处理图片 PDImageXObject image = PDImageXObject.createFromFile("path/to/your/image.png", document); PDFormXObject form = new PDFormXObject(document); form.setResources(new PDResources()); form.setBBox(new PDRectangle(image.getWidth(), image.getHeight())); try (PDPageContentStream contentStream = new PDPageContentStream(document, form)) { contentStream.drawImage(image, 0, 0, image.getWidth(), image.getHeight()); } // 设置链接外观与位置 PDRectangle linkRect = new PDRectangle(250, 550, image.getWidth(), image.getHeight()); txtLink.setRectangle(linkRect); PDAppearanceDictionary appearance = new PDAppearanceDictionary(); PDAppearanceEntry appearanceEntry = new PDAppearanceEntry(); appearanceEntry.setNormalAppearance(form); appearance.setNormalAppearance(appearanceEntry); txtLink.setAppearance(appearance); // 设置链接跳转动作 final PDActionURI actionURI = new PDActionURI(); actionURI.setURI("https://stackoverflow.com/"); txtLink.setAction(actionURI); // 隐藏边框 txtLink.setBorder(new float[]{0, 0, 0}); pdPage.getAnnotations().add(txtLink);
注意事项
- 确保图片路径正确,PDFBox支持常见图片格式;
- PDF页面坐标系以左下角为原点,需根据实际需求调整链接矩形的位置坐标;
- 若需要设置悬停、点击状态的不同图片,可分别配置外观字典的
R(滚过)、D(按下)状态。
内容的提问来源于stack exchange,提问作者Shahid Ghafoor
相关产品推荐
相关产品推荐

