PDF 文档的注释功能可以帮助用户添加评论、标记文本,可以在很多应用 PDF 文档的场景中使用,比如给文档添加反馈、给文档中的难懂词汇添加注释以及阅读文档时做笔记。用户可以在 PDF 文档中添加各种各样的注释,包括弹出式注释、文本框注释、链接式注释和线条注释等。本文将介绍如何使用 Spire.PDF for Java 通过简单代码实现在 PDF 文档中添加弹出式注释和文本框注释。
安装 Spire.PDF for Java
首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>10.7.3</version>
</dependency>
</dependencies>
在 PDF 文档中添加弹出式注释
弹出式注释在 PDF 页面上显示为按钮,而注释文字不会直接在页面上显示,鼠标光标移动到按钮上时才会显示注释文字。阅读者还可以直接打开 PDF 阅读器地“注释”界面直接查看注释。以下是在 PDF 文档中添加弹出式注释的操作步骤:
- 创建一个 PdfDocument 类的对象。
- 使用 PdfDocument.loadFromFile() 方法载入 PDF 文件。
- 使用 PdfDocument.loadFromFile() 方法获取文档第一页。
- 使用 PdfPageBase.findText().getFinds() 方法找到需要添加注释的文本。
- 创建 PdfPopupAnnotation 对象并设置注释的文本和位置。
- 使用 PdfPopupAnnotation 类下的方法设置注释的样式和颜色。
- 使用 PdfPageBase.getAnnotationsWidget().add() 方法将注释添加到第一页。
- 使用 PdfDocument.saveToFile() 方法保存文档。
- Java
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
import com.spire.pdf.general.find.PdfTextFind;
public class PDFPopUpAnnotation {
public static void main(String[] args) {
//创建PdfDocument类的对象
PdfDocument pdf = new PdfDocument();
//载入PDF文档
pdf.loadFromFile("生而如是.pdf");
//获取第一页
PdfPageBase page = pdf.getPages().get(0);
//查找需要注释的文字
PdfTextFind[] find = page.findText("成长学家").getFinds();
//创建PdfPopupAnnotation的对象并设置注释的文本和位置
String text = "成长学家是探索和总结儿童成长过程的研究人员。";
float a = (float)(find[0].getPosition().getX() + find[0].getSize().getWidth()-20);
float b = (float)(find[0].getPosition().getY() + find[0].getSize().getHeight());
Rectangle2D rectangle2D = new Rectangle.Float();
rectangle2D.setFrame(new Point2D.Double(a,b),new Dimension());
PdfPopupAnnotation annotation = new PdfPopupAnnotation(rectangle2D, text);
//设置注释的样式和颜色
annotation.setIcon(PdfPopupIcon.Note);
annotation.setColor(new PdfRGBColor(Color.red));
//将注释添加到第一页
page.getAnnotationsWidget().add(annotation);
//保存文档
pdf.saveToFile("弹出式注释.pdf");
pdf.close();
}
}
在 PDF 文档中添加文本框注释
文本框注释,也叫自由文本注释,由文本框以及文本框中的注释文字组成。文本框注释的注释文字直接显示在页面上的文本框中,同时也可以在 PDF 阅读器的“注释”界面看到。在 PDF 文档中添加文本框注释的操作步骤如下:
- 创建一个 PdfDocument 类的对象。
- 使用 PdfDocument.loadFromFile() 方法载入 PDF 文档。
- 使用 PdfDocument.getPages() 方法获取文档第一页。
- 使用 PdfPageBase.findText.getFinds() 方法查找要注释的文本。
- 创建一个 PdfFreeTextAnnotation 对象并设置注释的位置。
- 使用 PdfFreeTextAnnotation 类下的方法设置注释的文本、字体、文本颜色和文本框格式。
- 使用 PdfPageBase.getAnnotationsWidget().add() 方法将批注添加到页面中。
- 使用 PdfDocument.saveToFile() 方法保存文档。
- Java
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
import com.spire.pdf.general.find.PdfTextFind;
public class PDFTextBoxAnnotation {
public static void main(String[] args) {
//创建PdfDocument类的对象
PdfDocument doc = new PdfDocument();
//载入PDF文档
doc.loadFromFile("生而如是.pdf");
//获取第一页
PdfPageBase page = doc.getPages().get(0);
//查找要注释的文本
PdfTextFind[] find = page.findText("成长学家").getFinds();
//创建PdfFreeTextAnnotation对象并设置注释的位置
float x = (float)(find[0].getPosition().getX() + find[0].getSize().getWidth() - 20);
float y = (float)(find[0].getPosition().getY() + find[0].getSize().getHeight());
Rectangle2D.Float rect = new Rectangle2D.Float(x, y, 150, 30);
PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
//设置注释的文本、字体、字体颜色和文本框格式
textAnnotation.setMarkupText("成长学家是探索和总结儿童成长过程的研究人员。");
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("黑体",0, 12));
textAnnotation.setFont(font);
PdfAnnotationBorder border = new PdfAnnotationBorder(0.3f);
textAnnotation.setBorder(border);
textAnnotation.setBorderColor(new PdfRGBColor(Color.pink));
textAnnotation.setColor(new PdfRGBColor(Color.YELLOW));
textAnnotation.setOpacity(0.7f);
textAnnotation.setTextMarkupColor(new PdfRGBColor(Color.black));
//将注释添加到第一页
page.getAnnotationsWidget().add(textAnnotation);
//保存文档
doc.saveToFile("文本框注释.pdf");
doc.close();
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。