Map<String, PDAction> links = new HashMap<String, PDAction>();
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
List<PDAnnotation> annotations = page.getAnnotations();
// First setup the text extraction regions.
for (int j = 0; j < annotations.size(); j++) {
PDAnnotation annotation = annotations.get(j);
if (annotation instanceof PDAnnotationLink) {
PDAnnotationLink link = (PDAnnotationLink) annotation;
PDRectangle rect = link.getRectangle();
// Need to reposition link rectangle to match text space.
float x = rect.getLowerLeftX();
float y = rect.getUpperRightY();
float width = rect.getWidth();
float height = rect.getHeight();
int rotation = page.findRotation();
if (rotation == 0) {
PDRectangle pageSize = page.findMediaBox();
y = pageSize.getHeight() - y;
} else if (rotation == 90) {
// Do nothing.
}
Rectangle2D.Float awtRect = new Rectangle2D.Float(x, y, width, height);
stripper.addRegion(String.valueOf(j), awtRect);
}
}
stripper.extractRegions(page);
for (int j = 0; j < annotations.size(); j++) {
PDAnnotation annotation = annotations.get(j);
if (annotation instanceof PDAnnotationLink) {
PDAnnotationLink link = (PDAnnotationLink) annotation;
String label = stripper.getTextForRegion(String.valueOf(j)).trim();
links.put(label, link.getAction());
}