Usually, PDF files are loved
because they are secure and retain the format of information even when
compressed. However, they are not easy to edit. To do this effectively we can
add annotations for specified text in PDF. This article will use Java codes to demonstrate
how to add annotations to PDF and then how to delete them. The operation can be
done without Adobe Acrobat installed.
Development
Environment
Before running codes, we need to
download and install JDK and Intellij IDEA to create a development environment.
At the same time, here we need to use a free third-party library called Free Spire.PDF for Java.
Add
Spire.Pdf.jar to IDEA
Download the package of Free
Spire.Pdf for Java from the link, find a Jar file called Spire.Pdf.jar in the “lib”
folder, and then add it to IDEA. Or you can directly refer to it by using the
following Maven configurations.
<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>
Create a new PDF file and add a pop-up annotation to it
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class PopupAnnotation {
public static void main(String[] args) {
//Create a new PDF document
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.";
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 = "Popup 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 = "What is Annotation";
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("output/AddPopupAnnotation.pdf");
doc.close();
}
}
Output
Adding a text box annotation to an existing PDF file
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 TextBoxAnnotation {
public static void main(String[] args) {
//load the document from file
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");
//get the first page
PdfPageBase page = doc.getPages().get(0);
//search a string and get its location where to add the annotation
PdfTextFind[] find = page.findText("Cashless Society").getFinds();
float x = (float) (find[0].getPosition().getX() - doc.getPageSettings().getMargins().getLeft() + find[0].getSize().getWidth() + 20);
float y = (float) (find[0].getPosition().getY() - doc.getPageSettings().getMargins().getTop() + 20);
//define a text annotation
Rectangle2D.Float rect = new Rectangle2D.Float(x, y, 180, 30);
PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
// set the annotation text, font and format
textAnnotation.setMarkupText("The definition of Cashless Society");
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 0, 12));
;
textAnnotation.setFont(font);
PdfAnnotationBorder border = new PdfAnnotationBorder(0.5f);
textAnnotation.setBorder(border);
textAnnotation.setBorderColor(new PdfRGBColor(Color.pink));
textAnnotation.setColor(new PdfRGBColor(Color.YELLOW));
textAnnotation.setOpacity(0.75f);
textAnnotation.setTextMarkupColor(new PdfRGBColor(Color.black));
//add the annotation to the PDF page
page.getAnnotationsWidget().add(textAnnotation);
//Save the document to file
doc.saveToFile("output/FreeTextAnnotation.pdf");
doc.close();
}
}
Output
Delete annotations in PDFFree Spire.PDF for Java not only supports deleting a specified annotation but also it supports removing
all the annotations at one time.
import com.spire.pdf.*;
public class DeleteAnnotation {
public static void main(String[] args) {
//Load the sample PDF file
PdfDocument document = new PdfDocument();
document.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.pdf");
////Remove the first annotation from the first page of PDF
//document.getPages().get(0).getAnnotationsWidget().removeAt(0);
//Remove all annotations
document.getPages().get(0).getAnnotationsWidget().clear();
String result = "output/deleteAllAnnotations.pdf";
//Save the document
document.saveToFile(result);
}
}