Skip to content

Instantly share code, notes, and snippets.

@documentprocessing
Created June 10, 2025 06:47
Show Gist options
  • Select an option

  • Save documentprocessing/2076dbdd4ef1d981e7dc72d34e78faad to your computer and use it in GitHub Desktop.

Select an option

Save documentprocessing/2076dbdd4ef1d981e7dc72d34e78faad to your computer and use it in GitHub Desktop.
Add Annotations to PDF using PDFClown API in Java
// Import PDF Clown annotation and action classes
import org.pdfclown.documents.Document;
import org.pdfclown.documents.Page;
import org.pdfclown.documents.interaction.annotations.Link;
import org.pdfclown.documents.interaction.actions.GoToURI;
import org.pdfclown.files.File;
import org.pdfclown.documents.interaction.navigation.document.Destination;
import org.pdfclown.documents.interaction.navigation.document.LocalDestination;
import java.awt.geom.Rectangle2D;
public class AddLinkAnnotation {
public static void main(String[] args) {
try {
// 1. Load or create a PDF file
File pdfFile = new File();
Document document = pdfFile.getDocument();
// 2. Add a blank page
Page page = new Page(document);
document.getPages().add(page);
// 3. Create a link annotation (clickable area)
Rectangle2D linkBounds = new Rectangle2D.Double(50, 700, 200, 20); // x,y,width,height
Link link = new Link(page, linkBounds);
// 4. Set the link action (opens a URL)
GoToURI uriAction = new GoToURI(document, "https://github.com/stefanochizzolini/PDFClown");
link.setAction(uriAction);
// 5. Add descriptive text near the link
org.pdfclown.documents.contents.composition.PrimitiveComposer composer
= new org.pdfclown.documents.contents.composition.PrimitiveComposer(page);
composer.setFont(
new org.pdfclown.documents.contents.fonts.StandardType1Font(
document,
org.pdfclown.documents.contents.fonts.StandardType1Font.FamilyEnum.HELVETICA,
true,
false
),
12
);
composer.showText("Click here to visit PDF Clown GitHub!", new java.awt.geom.Point2D.Double(50, 720));
composer.flush();
// 6. Save the PDF
pdfFile.save("link_annotation.pdf");
System.out.println("PDF with link annotation created successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment