使用 Java 的交互式批注
Contents
[
Hide
]
本节中的交互式批注侧重于基于链接和按钮的工作流,这些工作流会响应 PDF 查看器中的用户操作。
添加链接批注
当需要在页面上的文字上放置可点击链接时,请使用此示例。
- 打开源 PDF Document。
- 定位目标文本片段,并在其矩形区域中创建一个 LinkAnnotation。
- 分配一个 GoToURIAction 并保存更新后的文档。
public static void linkAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("file");
document.getPages().get_Item(1).accept(textFragmentAbsorber);
var phoneNumberFragment = textFragmentAbsorber.getTextFragments().get_Item(1);
LinkAnnotation linkAnnotation = new LinkAnnotation(
document.getPages().get_Item(1),
phoneNumberFragment.getRectangle());
linkAnnotation.setAction(new GoToURIAction("https://www.aspose.com"));
document.getPages().get_Item(1).getAnnotations().add(linkAnnotation);
document.save(outputFile.toString());
}
}
获取链接批注
此示例扫描页面批注集合并报告每个链接批注的位置。
- 打开源 PDF Document。
- 遍历目标页面上的批注。
- 按条件筛选批注 AnnotationType.
Link并打印它们的矩形。
public static void linkGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Link) {
System.out.println(annotation.getRect());
}
}
}
}
删除链接批注
当需要从页面中移除现有链接批注时,请使用此方法。
- 打开源 PDF Document。
- 收集类型为 AnnotationType.
Link的批注。 - 删除收集的批注并保存输出文件。
public static void linkDelete(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.Link) {
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 应包含上一页和下一页按钮用于交互式导航时,请使用此示例。
- 打开源 PDF Document 并确保文档包含所需的页面。
- 创建带有预定义导航操作的 ButtonField 控件。
- 将按钮添加到表单集合中并保存更新后的文档。
public static void navigationButtonsAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
document.getPages().add();
record ButtonConfig(String name, double xPos, PredefinedAction action) {}
List<ButtonConfig> buttonConfigs = List.of(
new ButtonConfig("Previous Page", 120.0, PredefinedAction.PrevPage),
new ButtonConfig("Next Page", 230.0, PredefinedAction.NextPage));
for (Page page : document.getPages()) {
for (ButtonConfig config : buttonConfigs) {
Rectangle rect = new Rectangle(config.xPos(), 10.0, config.xPos() + 100, 40.0, true);
ButtonField button = new ButtonField(page, rect);
button.setPartialName(config.name());
button.setValue(config.name());
button.getCharacteristics().setBorder(Color.getRed());
button.getCharacteristics().setBackground(Color.getOrange().toRgb());
button.getAnnotationActions().setOnReleaseMouseBtn(new NamedAction(config.action()));
document.getForm().add(button);
}
}
document.save(outputFile.toString());
}
}
添加打印按钮
此示例创建了一个按钮,当用户点击时触发打印命令。
- 创建新的 PDF Document 并添加一个页面。
- 创建一个 ButtonField 并分配打印预定义操作。
- 配置按钮的边框和背景,将其添加到表单中,并保存文档。
public static void printButtonAdd(Path outputFile) {
try (Document document = new Document()) {
Page page = document.getPages().add();
Rectangle rect = new Rectangle(72, 748, 164, 768, true);
ButtonField printButton = new ButtonField(page, rect);
printButton.setAlternateName("Print current document");
printButton.setColor(Color.getBlack());
printButton.setPartialName("printBtn1");
printButton.setValue("Print Document");
printButton.getAnnotationActions().setOnReleaseMouseBtn(
new NamedAction(PredefinedAction.File_Print));
Border border = new Border(printButton);
border.setStyle(BorderStyle.Solid);
border.setWidth(2);
printButton.setBorder(border);
printButton.getCharacteristics().setBorder(Color.getBlue());
printButton.getCharacteristics().setBackground(Color.getLightBlue().toRgb());
document.getForm().add(printButton);
document.save(outputFile.toString());
}
}