DEV Community

loading...

Add Annotations to PDF in Java

eiceblue profile image E-iceblue Product Family ・2 min read

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 and add it as a dependency in your application.

<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>
Enter fullscreen mode Exit fullscreen mode

Example 1. Add Text Markup Annotation

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Example 2. Add Pop-up Annotation

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Discussion (0)

pic
Editor guide