评论处理器
大约 2 分钟pdfbox模块高级功能处理器
提示
操作完评论后,务必调用 flush 方法刷新评论,否则不生效
说明
用于处理评论
- 支持获取评论
- 支持添加评论
- 支持替换评论
- 支持移除评论
示例
获取评论
- 获取评论字典
说明
返回值为 map ,key 为页面索引, value 为 评论列表
try (
// 加载文档
Document document = PdfHandler.getDocumentHandler().load("E:\\PDF\\pdfbox\\hello-world.pdf");
){
// 获取处理器
CommentProcessor processor = PdfHandler.getDocumentProcessor(document).getCommentProcessor();
// 获取评论字典
Map<Integer, List<PDAnnotation>> map = processor.getMap();
// 输出评论信息
map.forEach((key, value) -> System.out.println("第" + key + "页:" + value));
}
- 获取评论列表
try (
// 加载文档
Document document = PdfHandler.getDocumentHandler().load("E:\\PDF\\pdfbox\\hello-world.pdf");
){
// 获取处理器
CommentProcessor processor = PdfHandler.getDocumentProcessor(document).getCommentProcessor();
// 获取评论列表
List<PDAnnotation> list = processor.getList();
// 输出评论信息
list.forEach(System.out::println);
}
添加评论
try (
// 加载文档
Document document = PdfHandler.getDocumentHandler().load("E:\\PDF\\pdfbox\\hello-world.pdf");
){
// 获取处理器
CommentProcessor processor = PdfHandler.getDocumentProcessor(document).getCommentProcessor();
// 创建评论
PDAnnotationFreeText commentAnnotation = new PDAnnotationFreeText();
// 设置名称
commentAnnotation.setAnnotationName(IdUtil.get());
// 设置内容
commentAnnotation.setContents("测试");
// 设置背景颜色
commentAnnotation.setColor(ColorUtil.toPDColor(Color.CYAN));
// 设置作者
commentAnnotation.setTitlePopup("xsx");
// 设置主题
commentAnnotation.setSubject("主题");
// 设置日期
commentAnnotation.setModifiedDate((new GregorianCalendar()));
// 设置范围
commentAnnotation.setRectangle(Size.create(200F, 300F, 200F, 240F).getRectangle());
// 添加评论
processor.add(0, "宋体", commentAnnotation);
// 保存文档
document.save("E:\\PDF\\pdfbox\\processor\\comment\\addTest.pdf");
}
替换评论
try (
// 加载文档
Document document = PdfHandler.getDocumentHandler().load("E:\\PDF\\pdfbox\\hello-world.pdf");
){
// 获取处理器
CommentProcessor processor = PdfHandler.getDocumentProcessor(document).getCommentProcessor();
// 创建评论
PDAnnotationFreeText commentAnnotation = new PDAnnotationFreeText();
// 设置名称
commentAnnotation.setAnnotationName(IdUtil.get());
// 设置内容
commentAnnotation.setContents("测试");
// 设置背景颜色
commentAnnotation.setColor(ColorUtil.toPDColor(Color.CYAN));
// 设置作者
commentAnnotation.setTitlePopup("xsx");
// 设置主题
commentAnnotation.setSubject("主题");
// 设置日期
commentAnnotation.setModifiedDate((new GregorianCalendar()));
// 设置范围
commentAnnotation.setRectangle(Size.create(200F, 300F, 200F, 240F).getRectangle());
// 替换评论
processor.set(0, "test", commentAnnotation);
// 保存文档
document.save("E:\\PDF\\pdfbox\\processor\\comment\\setTest.pdf");
}
移除评论
try (
// 加载文档
Document document = PdfHandler.getDocumentHandler().load("E:\\PDF\\pdfbox\\hello-world.pdf");
){
// 获取处理器
CommentProcessor processor = PdfHandler.getDocumentProcessor(document).getCommentProcessor();
// 移除评论
processor.remove(0, "test", 0);
// 保存文档
document.save("E:\\PDF\\pdfbox\\processor\\comment\\removeTest.pdf");
}