基于文本的批注(使用 Java)
Contents
[
Hide
]
基于文本的批注使审阅者和开发人员能够向 PDF 文档添加交互式批注、突出显示和标记,而不更改核心内容。本节介绍了在文档审阅工作流、合规情景和协作反馈周期中使用的五种实用批注类型。
快速参考:批注类型
本文涵盖以下基于文本的批注类型:
- Free Text:可编辑的文本框,用于添加批注和评论
- 高亮: 对重要文本段落的视觉强调
- Strikeout: 在审阅期间将文本标记为删除或修订
- Squiggly:用于指示错误或关注的波浪下划线
- Underline: 传统的下划线强调,可选四点精度
添加、获取和删除自由文本批注
自由文本批注作为浮动文本框,可以在不影响文档结构的情况下进行编辑。使用这些示例可添加评论框、检查其属性或将其删除。
添加自由文本批注
- 打开源 PDF Document。
- 创建一个带有矩形和外观设置的 FreeTextAnnotation。
- 将批注添加到页面并保存文档。
public static void freeTextAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
FreeTextAnnotation freeTextAnnotation = new FreeTextAnnotation(
document.getPages().get_Item(1),
new Rectangle(299, 713, 308, 720, true),
new DefaultAppearance());
freeTextAnnotation.setTitle("Aspose User");
freeTextAnnotation.setColor(Color.getLightGreen());
document.getPages().get_Item(1).getAnnotations().add(freeTextAnnotation);
document.save(outputFile.toString());
}
}
获取自由文本批注
- 打开源 PDF Document。
- 遍历页面上的批注并进行筛选 AnnotationType.FreeText。
- 检索批注属性或边界。
public static void freeTextAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.FreeText) {
System.out.println(annotation.getRect());
}
}
}
}
删除自由文本批注
- 打开源 PDF Document。
- 通过遍历页面批注并按类型过滤,查找自由文本批注。
- 将匹配的批注添加到删除列表并从页面中移除它们。
- 保存已更新的文档。
public static void freeTextAnnotationDelete(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.FreeText) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
添加、获取和删除高亮批注
突出显示批注使用半透明覆盖层标记重要段落。使用这些示例为文档审阅创建高亮,定位已有的高亮,并清理标记。
添加高亮批注
- 打开源 PDF Document。
- 创建一个 HighlightAnnotation 使用矩形定义突出显示区域。
- 将批注添加到页面并保存文档。
public static void textHighlightAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HighlightAnnotation highlightAnnotation = new HighlightAnnotation(
document.getPages().get_Item(1),
new Rectangle(300, 750, 320, 770, true));
document.getPages().get_Item(1).getAnnotations().add(highlightAnnotation);
document.save(outputFile.toString());
}
}
获取高亮批注
- 打开源 PDF Document。
- 遍历批注并按 AnnotationType.Highlight。
- 读取批注属性,如边界或颜色。
public static void textHighlightAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Highlight) {
System.out.println(annotation.getRect());
}
}
}
}
删除高亮批注
- 打开源 PDF Document。
- 通过按类型过滤批注来收集高亮批注。
- 从页面中删除每个批注。
- 保存已更新的文档。
public static void textHighlightAnnotationDelete(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.Highlight) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
添加、获取和删除删除线批注
删除线批注用于在文本上划线,以指示删除、拒绝或修改。使用这些示例在文档审阅期间应用删除线标记、查找已标记的文本,并删除删除线批注。
添加删除线批注
- 打开源 PDF Document。
- 创建一个带有矩形、标题和颜色的 StrikeOutAnnotation。
- 将批注添加到页面并保存文档。
public static void textStrikeoutAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
StrikeOutAnnotation strikeoutAnnotation = new StrikeOutAnnotation(
document.getPages().get_Item(1),
new Rectangle(299.988, 713.664, 308.708, 720.769, true));
strikeoutAnnotation.setTitle("Aspose User");
strikeoutAnnotation.setSubject("Inserted text 1");
strikeoutAnnotation.setFlags(AnnotationFlags.Print);
strikeoutAnnotation.setColor(Color.getBlue());
document.getPages().get_Item(1).getAnnotations().add(strikeoutAnnotation);
document.save(outputFile.toString());
}
}
获取删除线批注
- 打开源 PDF Document。
- 遍历批注并按 AnnotationType.StrikeOut。
- 读取批注元数据或边界。
public static void textStrikeoutAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.StrikeOut) {
System.out.println(annotation.getRect());
}
}
}
}
删除删除线批注
- 打开源 PDF Document。
- 通过按类型过滤来收集删除线批注。
- 从页面中删除每个批注。
- 保存已更新的文档。
public static void textStrikeoutAnnotationDelete(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.StrikeOut) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
添加、获取和删除波浪线批注
波浪线批注(波形下划线)用于突出显示潜在错误、关注点或需要注意的项目。使用这些示例来标记有问题的文本、检查波浪线批注,并从文档中移除它们。
添加波浪线批注
- 打开源 PDF Document。
- 创建一个带有矩形和标题的 SquigglyAnnotation。
- 将批注添加到页面并保存文档。
public static void textSquigglyAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
SquigglyAnnotation squigglyAnnotation = new SquigglyAnnotation(
page,
new Rectangle(67, 317, 261, 459, true));
squigglyAnnotation.setTitle("John Smith");
squigglyAnnotation.setColor(Color.getBlue());
page.getAnnotations().add(squigglyAnnotation);
document.save(outputFile.toString());
}
}
获取波浪线批注
- 打开源 PDF Document。
- 遍历批注并按 AnnotationType.Squiggly。
- 读取批注的边界或元数据。
public static void textSquigglyAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Squiggly) {
System.out.println(annotation.getRect());
}
}
}
}
删除波浪线批注
- 打开源 PDF Document。
- 通过按类型过滤来收集波浪线批注。
- 从页面中删除每个批注。
- 保存已更新的文档。
public static void textSquigglyAnnotationDelete(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.Squiggly) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
添加、获取和删除下划线批注
下划线批注通过传统的下划线强调重要段落。使用这些示例来创建下划线、读取标记的文本内容以及删除页面中的下划线批注。
添加下划线批注
- 打开源 PDF Document。
- 创建一个带有矩形和颜色的 UnderlineAnnotation。
- 将批注添加到页面并保存文档。
public static void textUnderlineAnnotationAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
document.getPages().get_Item(1),
new Rectangle(299.988, 713.664, 308.708, 720.769, true));
underlineAnnotation.setTitle("Aspose User");
underlineAnnotation.setSubject("Inserted Underline 1");
underlineAnnotation.setFlags(AnnotationFlags.Print);
underlineAnnotation.setColor(Color.getBlue());
document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
document.save(outputFile.toString());
}
}
获取下划线批注
- 打开源 PDF Document。
- 遍历批注并按 AnnotationType.Underline。
- 读取批注属性或边界。
public static void textUnderlineAnnotationGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Underline) {
System.out.println(annotation.getRect());
}
}
}
}
删除下划线批注
- 打开源 PDF Document。
- 通过按类型过滤收集下划线批注。
- 从页面中删除每个批注。
- 保存已更新的文档。
public static void textUnderlineAnnotationDelete(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.Underline) {
toDelete.add(annotation);
}
}
for (Annotation annotation : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(annotation);
}
document.save(outputFile.toString());
}
}
添加带四点坐标的下划线批注
此示例通过从矩形派生的四点显式定义下划线区域。
- 打开源 PDF Document。
- 创建一个 UnderlineAnnotation 并计算其四点坐标。
- 将批注添加到页面并保存文档。
public static void textUnderlineWithQuadPointsAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Rectangle rect = new Rectangle(299.988, 713.664, 308.708, 720.769, true);
UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
document.getPages().get_Item(1), rect);
underlineAnnotation.setTitle("Aspose User");
underlineAnnotation.setSubject("Inserted Underline with Quad Points");
underlineAnnotation.setFlags(AnnotationFlags.Print);
underlineAnnotation.setColor(Color.getBlue());
underlineAnnotation.setQuadPoints(new com.aspose.pdf.Point[]{
new com.aspose.pdf.Point(rect.getLLX(), rect.getLLY()),
new com.aspose.pdf.Point(rect.getURX(), rect.getLLY()),
new com.aspose.pdf.Point(rect.getURX(), rect.getURY()),
new com.aspose.pdf.Point(rect.getLLX(), rect.getURY())
});
document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
document.save(outputFile.toString());
}
}
获取下划线批注中的标记文本
检索下划线批注覆盖的实际文本内容。这些示例展示了两种方法:将完整的标记文本读取为单个字符串,或逐个处理文本片段进行详细分析。
- 打开源 PDF Document。
- 遍历页面上的下划线批注。
- 读取任意一个
getMarkedText()或getMarkedTextFragments()并打印结果。
public static void textUnderlineMarkedTextGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Underline) {
UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
System.out.println("Marked text: " + ua.getMarkedText());
}
}
}
}
public static void textUnderlineMarkedFragmentsGet(Path inputFile) {
try (Document document = new Document(inputFile.toString())) {
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Underline) {
UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
for (TextFragment fragment : ua.getMarkedTextFragments()) {
System.out.println("Fragment text: " + fragment.getText());
}
}
}
}
}
按标题删除下划线批注
通过过滤元数据属性(如标题)有选择地删除批注。此方法可以按作者或用途有针对性地清理批注。
- 打开源 PDF Document。
- 按标题过滤下划线批注。
- 删除匹配的批注并保存更新后的文档。
public static void textUnderlineByTitleDelete(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<UnderlineAnnotation> toDelete = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Underline) {
UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
if ("Aspose User".equals(ua.getTitle())) {
toDelete.add(ua);
}
}
}
for (UnderlineAnnotation ua : toDelete) {
document.getPages().get_Item(1).getAnnotations().delete(ua);
}
document.save(outputFile.toString());
}
}
添加并展平下划线批注
通过扁平化将交互式下划线批注转换为永久页面内容。这可防止进一步编辑,同时在任何 PDF 查看器中保留下划线的外观。
- 打开源 PDF Document。
- 将一个 UnderlineAnnotation 添加到页面。
- 调用
flatten()在批注上并保存输出文件。
public static void textUnderlineFlattenAdd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
document.getPages().get_Item(1),
new Rectangle(299.988, 713.664, 308.708, 720.769, true));
underlineAnnotation.setTitle("Aspose User");
underlineAnnotation.setSubject("Inserted Underline to Flatten");
underlineAnnotation.setFlags(AnnotationFlags.Print);
underlineAnnotation.setColor(Color.getBlue());
document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
underlineAnnotation.flatten();
document.save(outputFile.toString());
}
}