Java: Add Pop-Up and Text Box Annotations to PDF

PDF annotations help users make comments and mark up text. They are used in many situations where PDF files are involved, such as giving feedback on reports, making explanations for difficult words, and taking notes while reading articles. Users can add many types of annotations to PDF documents, such as pop-up, text box, link, and line annotations. This article will show how to add pop-up and text box annotations in PDF documents using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>10.3.4</version>
    </dependency>
</dependencies>
    

Add a Pop-Up Annotation to a PDF Document

Pop-up annotations are displayed as buttons in PDF documents. They do not show annotation text on PDF pages, but only in the Comments widget of PDF viewers. The detailed steps for adding a pop-up annotation to a PDF document are as follows.

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the first page of the document using PdfDocument.getPages().get() method.
  • Find the text to annotate using PdfPageBase.findText().getFinds() method.
  • Create a PdfPopupAnnotation object and set the text and position of the annotation.
  • Set the style and color of the annotation using methods under PdfPopupAnnotation class.
  • Add the annotation to the page using PdfPageBase.getAnnotationsWidget().add() method.
  • Save the document using PdfDocument.saveToFile() method.
  • 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)  {

        //Create an instance of PdfDocument class
        PdfDocument pdf = new PdfDocument();

        //Load a PDF Document
        pdf.loadFromFile("Born That Way.pdf");

        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);

        //Find the text to add annotation
        PdfTextFind[] find = page.findText("developmental scientists").getFinds();

        //Create a PdfPopupAnnotation object and set the text and position of the annotation
        String text = "Developmental scientists are researchers who explores and summarizes the growth process of children.";
        float a = (float)(find[0].getPosition().getX() + find[0].getSize().getWidth() - 20);
        float b = (float)(find[0].getPosition().getY());
        Rectangle2D rectangle2D = new Rectangle.Float();
        rectangle2D.setFrame(new Point2D.Double(a,b),new Dimension());
        PdfPopupAnnotation annotation = new PdfPopupAnnotation(rectangle2D, text);

        //Set the text, position, and style of the annotation
        annotation.setIcon(PdfPopupIcon.Note);
        annotation.setColor(new PdfRGBColor(Color.red));

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

        //Save the document
        pdf.saveToFile("PopUpAnnotation.pdf");
        pdf.close();
    }
}

Java: Add Pop-Up and Text Box Annotations to PDF

Add a Text Box Annotation to a PDF Document

Pop-up annotations are displayed as buttons on PDF pages. The annotation text of pop-up annotations is not displayed directly on the page, but it will appear on the page when the mouse cursor is moved to the button. Readers can also directly open the "Comments" interface to view pop-up annotations directly. The detailed steps to add a text box annotation to a PDF document are as follows.

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the first page using PdfDocument.getPages().get() method.
  • Find the text to annotate using PdfPageBase.findText.getFinds() method.
  • Create a PdfFreeTextAnnotation object and set the position of the annotation.
  • Set the text, font, text color, and text box format of the annotation using methods under PdfFreeTextAnnotation class.
  • Add the annotation to the page using PdfPageBase.getAnnotationsWidget().add() method.
  • Save the document using PdfDocument.saveToFile() method.
  • 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)  {

        //Create an instance of PdfDocument class 
        PdfDocument doc = new PdfDocument();

        //Load a PDF document
        doc.loadFromFile("Born That Way.pdf");

        //Get the first page
        PdfPageBase page = doc.getPages().get(0);

        //Find the text to annotate
        PdfTextFind[] find = page.findText("developmental scientists").getFinds();

        //Create a PdfFreeTextAnnotation object and set the position of the annotation
        float x = (float)(find[0].getPosition().getX() + find[0].getSize().getWidth());
        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);

        //Set the text, font, text color, and text box format of the annotation
        textAnnotation.setMarkupText("Scientists who research the growth process of children.");
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial",0, 11));
        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));

        //Add the annotation to the page
        page.getAnnotationsWidget().add(textAnnotation);

        //Save the document
        doc.saveToFile("TextBoxAnnotation.pdf");
        doc.close();
    }
}

Java: Add Pop-Up and Text Box Annotations to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.