Add Annotations to PDF in Java
Tags: java, pdf, annotation
Published: 2021-02-26 02:44:20

---
title: Add Annotations to PDF in Java
published: true
description: This article demonstrates how to add annotations to PDF in Java.
tags: java,pdf,annotation
//cover_image: https://direct_url_to_image.jpg
---
Increasingly, busy professionals receive various types of written content in the form of Portable Document Format (PDF) files. This content might be in the form of contracts, financial statements, reports, and even manuals. Often, recipients of this content would like to add notes, highlights, comments, and other markups to these PDF documents. In this article, I am going to introduce how to add text markup annotations and pop-up annotations to PDF by using Free Spire.PDF for Java. 

##Install Spire.Pdf.jar

If you’re creating a Maven project, you can easily add the jar in your application using the following configurations. For non-Maven projects, download the jar file from [this link](https://www.e-iceblue.com/Download/pdf-for-java-free.html) and add it as a dependency in your application.
```java
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>
```

##Example 1. Add Text Markup Annotation
```java
import com.spire.pdf.annotations.PdfAnnotationBorder;
import com.spire.pdf.annotations.PdfTextMarkupAnnotation;
import com.spire.pdf.general.find.PdfTextFind;
import com.spire.pdf.graphics.PdfFont;
import com.spire.pdf.graphics.PdfFontFamily;
import com.spire.pdf.graphics.PdfRGBColor;

import java.awt.*;

public class TextMarkup {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load the sample PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.pdf");

        //Get the first page
        PdfPageBase page = doc.getPages().get(0);
        
        //Find the string to add annotation
        PdfTextFind[] results = page.findText("such as security setting").getFinds();

        //Create a font
        PdfFont font = new PdfFont(PdfFontFamily.Times_Roman, 12);
        
        //Create a PdfTextMarkupAnnotation based on the searched string
        PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Administrator", "This is a text markup annotation.", results[0].getSearchText(), results[0].getPosition(),font);
        annotation.setBorder(new PdfAnnotationBorder(0.5f));
        annotation.setTextMarkupColor(new PdfRGBColor(Color.GREEN));

        //Add annotation to PDF
        page.getAnnotationsWidget().add(annotation);

        //Save to file
        doc.saveToFile("TextMarkup.pdf");
    }
}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fgir9jn408asn5ndxsb.png)
##Example 2. Add Pop-up Annotation
```java
import com.spire.pdf.annotations.PdfPopupAnnotation;
import com.spire.pdf.annotations.PdfPopupIcon;
import com.spire.pdf.general.find.PdfTextFind;
import com.spire.pdf.graphics.PdfRGBColor;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class PopupAnnotation {
    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load the sample PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.pdf");

        //Get the first page
        PdfPageBase page = doc.getPages().get(0);
        
        //Find the string to add annotation
        PdfTextFind[] results = page.findText("high quality.").getFinds();
        
        //Specify the x and y coordinate to add annotation
        float x = (float)results[0].getBounds().getMaxX(); 
        float y = (float) results[0].getBounds().getY();

        //Create a PdfPopupAnnotation object
        PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(new Rectangle2D.Float(x,y,0,0));
        popupAnnotation.setText("This is a pop-up annotation");
        popupAnnotation.setIcon(PdfPopupIcon.Note);
        popupAnnotation.setColor(new PdfRGBColor(Color.YELLOW));

        //Add annotation to PDF
        page.getAnnotationsWidget().add(popupAnnotation);

        //Save to file
        doc.saveToFile("PopupAnnotation.pdf");
    }
}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38lw60pr29i73vs3n0ul.png)