Text Based Annotations using Java
Text-based annotation workflows in this section cover free text, highlight, strikeout, squiggly, and underline scenarios.
Add, get, and delete free text annotations
Use these examples when you need to place editable text notes, inspect them, or remove them from the page.
- Open the source PDF Document.
- Create, find, or collect FreeTextAnnotation objects on the page.
- Save the updated document when adding or deleting annotations.
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());
}
}
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());
}
}
}
}
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());
}
}
Add, get, and delete highlight annotations
These examples show how to create highlight markup, inspect existing highlight annotations, and remove them.
- Open the source PDF Document.
- Work with HighlightAnnotation objects on the page.
- Save the document after adding or deleting the annotation.
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());
}
}
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());
}
}
}
}
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());
}
}
Add, get, and delete strikeout annotations
Use these examples when you need review-style strikeout markup over text ranges.
- Open the source PDF Document.
- Create, inspect, or collect StrikeOutAnnotation objects.
- Save the document after applying changes.
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());
}
}
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());
}
}
}
}
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());
}
}
Add, get, and delete squiggly annotations
These examples work with squiggly markup used to emphasize text during review.
- Open the source PDF Document.
- Create, inspect, or collect SquigglyAnnotation objects.
- Save the document after adding or deleting annotations.
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());
}
}
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());
}
}
}
}
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());
}
}
Add, get, and delete underline annotations
Use these examples when text should be underlined, inspected, or removed through annotation APIs.
- Open the source PDF Document.
- Work with UnderlineAnnotation objects on the page.
- Save the document after adding or deleting annotations.
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());
}
}
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());
}
}
}
}
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());
}
}
Add an underline annotation with quad points
This example defines the underline area explicitly through quad points derived from a rectangle.
- Open the source PDF Document.
- Create an UnderlineAnnotation and calculate its quad points.
- Add the annotation to the page and save the document.
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());
}
}
Get marked text from underline annotations
These examples read the text content associated with underline annotations, either as a full string or as individual fragments.
- Open the source PDF Document.
- Iterate through underline annotations on the page.
- Read either
getMarkedText()orgetMarkedTextFragments()and print the results.
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());
}
}
}
}
}
Delete underline annotations by title
Use this approach when underline annotations should be removed selectively based on their metadata.
- Open the source PDF Document.
- Filter underline annotations by title.
- Delete the matching annotations and save the updated 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());
}
}
Add and flatten an underline annotation
This example adds an underline annotation and immediately flattens it into static page content.
- Open the source PDF Document.
- Add an UnderlineAnnotation to the page.
- Call
flatten()on the annotation and save the output file.
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());
}
}