通过 Java 的形状批注
Contents
[
Hide
]
本节中的形状批注涵盖几何批注类型,例如方形、圆形、多边形、折线和直线。
添加方形、圆形、多边形和折线批注
在需要使用自定义颜色、不透明度、弹出数据或点数组放置几何批注时,请使用这些示例。
- 打开源 PDF Document。
- 创建所需的形状批注并配置其矩形、点和视觉属性。
- 将批注添加到页面并保存更新后的文档。
public static void squareAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
SquareAnnotation squareAnnotation = new SquareAnnotation(
document.getPages().get_Item(1),
new Rectangle(60, 600, 250, 450, true));
squareAnnotation.setTitle("John Smith");
squareAnnotation.setColor(Color.getBlue());
squareAnnotation.setInteriorColor(Color.getBlueViolet());
squareAnnotation.setOpacity(0.25);
document.getPages().get_Item(1).getAnnotations().add(squareAnnotation);
document.save(outputFile.toString());
}
}
public static void circleAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
CircleAnnotation circleAnnotation = new CircleAnnotation(
document.getPages().get_Item(1),
new Rectangle(270, 160, 483, 383, true));
circleAnnotation.setTitle("John Smith");
circleAnnotation.setColor(Color.getRed());
circleAnnotation.setInteriorColor(Color.getMistyRose());
circleAnnotation.setOpacity(0.5);
circleAnnotation.setPopup(new PopupAnnotation(
document.getPages().get_Item(1),
new Rectangle(842, 316, 1021, 459, true)));
document.getPages().get_Item(1).getAnnotations().add(circleAnnotation);
document.save(outputFile.toString());
}
}
public static void polygonAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
PolygonAnnotation polygonAnnotation = new PolygonAnnotation(
document.getPages().get_Item(1),
new Rectangle(200, 300, 400, 400, true),
new Point[]{
new Point(200, 300),
new Point(220, 300),
new Point(250, 330),
new Point(300, 304),
new Point(300, 400)
});
polygonAnnotation.setTitle("John Smith");
polygonAnnotation.setColor(Color.getBlue());
polygonAnnotation.setInteriorColor(Color.getBlueViolet());
polygonAnnotation.setOpacity(0.25);
document.getPages().get_Item(1).getAnnotations().add(polygonAnnotation);
document.save(outputFile.toString());
}
}
public static void polylineAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
PolylineAnnotation polylineAnnotation = new PolylineAnnotation(
document.getPages().get_Item(1),
new Rectangle(270, 193, 571, 383, true),
new Point[]{
new Point(545, 150),
new Point(545, 190),
new Point(667, 190),
new Point(667, 110),
new Point(626, 111)
});
polylineAnnotation.setTitle("John Smith");
polylineAnnotation.setColor(Color.getRed());
polylineAnnotation.setPopup(new PopupAnnotation(
document.getPages().get_Item(1),
new Rectangle(842, 196, 1021, 338, true)));
document.getPages().get_Item(1).getAnnotations().add(polylineAnnotation);
document.save(outputFile.toString());
}
}
获取方形、圆形、多边形和折线批注
这些示例检查页面批注集合,并按类型打印几何批注的矩形。
- 打开源 PDF Document。
- 遍历页面批注。
- 按必需项筛选 AnnotationType 获取值并打印矩形。
public static void squareAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Square) {
System.out.println(annotation.getRect());
}
}
}
}
public static void circleAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Circle) {
System.out.println(annotation.getRect());
}
}
}
}
public static void polygonAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Polygon) {
System.out.println(annotation.getRect());
}
}
}
}
public static void polylineAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.PolyLine) {
System.out.println(annotation.getRect());
}
}
}
}
删除方形、圆形、多边形和折线批注
当需要从页面中删除特定类型的形状批注时,请使用这些示例。
- 打开源 PDF Document。
- 收集所需几何类型的批注。
- 删除收集的批注并保存输出文件。
public static void squareAnnotationDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<Annotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Square) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
public static void circleAnnotationDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<Annotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Circle) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
public static void polygonAnnotationDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<Annotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Polygon) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
public static void polylineAnnotationDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<Annotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.PolyLine) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
添加直线批注
此示例创建了一个带有箭头结束、边框格式和弹出批注的线批注。
- 打开源 PDF Document。
- 创建一个带有起点和终点的 LineAnnotation。
- 配置外观,添加弹出窗口,并保存文档。
public static void lineAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
LineAnnotation lineAnnotation = new LineAnnotation(
document.getPages().get_Item(1),
new Rectangle(550, 93, 562, 439, true),
new Point(556, 99),
new Point(556, 443));
lineAnnotation.setTitle("John Smith");
lineAnnotation.setColor(Color.getRed());
lineAnnotation.setStartingStyle(LineEnding.OpenArrow);
lineAnnotation.setEndingStyle(LineEnding.OpenArrow);
Border border = new Border(lineAnnotation);
border.setWidth(3);
lineAnnotation.setBorder(border);
PopupAnnotation popup = new PopupAnnotation(
document.getPages().get_Item(1),
new Rectangle(842, 124, 1021, 266, true));
lineAnnotation.setPopup(popup);
document.getPages().get_Item(1).getAnnotations().add(lineAnnotation);
document.save(outputFile.toString());
}
}
获取线批注
此示例读取线批注并打印其起始和结束坐标。
- 打开源 PDF Document。
- 遍历页面批注并选择 AnnotationType.
Line。 - 将每个匹配转换为 LineAnnotation 并打印其坐标。
public static void lineAnnotationsGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Line) {
LineAnnotation la = (LineAnnotation) annotation;
System.out.printf("[%s,%s]-[%s,%s]%n",
la.getStarting().getX(), la.getStarting().getY(),
la.getEnding().getX(), la.getEnding().getY());
}
}
}
}
删除线批注
当需要从页面中移除线批注时,请使用此方法。
- 打开源 PDF Document。
- 收集特定类型的批注 AnnotationType.
Line。 - 删除已收集的批注并保存文档。
public static void lineAnnotationsDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
List<Annotation> toDelete = new ArrayList<>();
for (Annotation annotation : page.getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Line) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
page.getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}