DEV Community

carlwils
carlwils

Posted on • Updated on

Add Annotation to PDF in Java

While viewing a PDF file, you can add notes to the file to share your ideas or leave a comment. This article will share how to programmatically add a text box annotation and pop-up annotation to a PDF file using Free Spire.PDF for Java. In addition to that, the free library also supports adding text annotation, file link annotation, document link annotation, etc.

Installation (2 methods)

Method 1: Download the free library and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.

Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Samle Code

import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;

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

        PdfDocument doc = new PdfDocument();
        //Set the margin of PDF
        PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
        PdfMargins margin = new PdfMargins();
        margin.setTop(unitCvtr.convertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));
        margin.setBottom(margin.getTop());
        margin.setLeft(unitCvtr.convertUnits(3f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));
        margin.setRight(margin.getLeft());
        //Create one page
        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);

        //Add text
        PdfBrush brush1 = PdfBrushes.getBlack();
        PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", Font.BOLD + Font.ITALIC,13), true);
        PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Left);
        float y = 50;
        String s = "The sample demonstrates how to add annotations to PDF document.";
        page.getCanvas().drawString(s, font1, brush1, 0, y - 5, format1);
        y = y + (float)font1.measureString(s, format1).getHeight();

        //set the annotation font, string, size, position and style
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial",0, 12));
        PdfStringFormat format = new PdfStringFormat();
        format.setMeasureTrailingSpaces(true);
        String prompt = "Pop-up Annotation: ";
        Dimension2D size = font.measureString(prompt, format);
        page.getCanvas().drawString(prompt, font, PdfBrushes.getDodgerBlue(), 0, y);
        float x = (float)size.getWidth();
        String label = "demo of Pop-up annotation";
        page.getCanvas().drawString(label, font, PdfBrushes.getOrangeRed(), x, y);
        x = x + (float)font.measureString(label, format).getWidth();
        String markupText = "Please indicate the theme of this paragraph";
        Rectangle2D rectangle2D = new Rectangle.Float();
        rectangle2D.setFrame(new Point2D.Double(x,y),new Dimension());
        PdfPopupAnnotation annotation = new PdfPopupAnnotation(rectangle2D, markupText);
        annotation.setIcon(PdfPopupIcon.Paragraph);
        annotation.setOpen(true);
        annotation.setColor(new PdfRGBColor(Color.YELLOW));
        ((PdfNewPage) page).getAnnotations().add(annotation);

        //Save the document to file
        doc.saveToFile("annotation.pdf");
        doc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

PDFAnnotation

Discussion (0)