boolean annotsAllowed = pdfDoc.getProfile().isAnnotationAllowed();
// stuff we only need if a link must be created:
Rectangle2D ipRect = null;
PDFFactory factory = null;
PDFAction action = null;
if (annotsAllowed) {
// make sure the rect is determined *before* calling super!
int ipp = currentIPPosition;
int bpp = currentBPPosition + ip.getOffset();
ipRect = new Rectangle2D.Float(ipp / 1000f, bpp / 1000f,
ip.getIPD() / 1000f, ip.getBPD() / 1000f);
AffineTransform transform = currentState.getTransform();
ipRect = transform.createTransformedShape(ipRect).getBounds2D();
factory = pdfDoc.getFactory();
}
// render contents
super.renderInlineParent(ip);
boolean linkTraitFound = false;
// try INTERNAL_LINK first
Trait.InternalLink intLink = (Trait.InternalLink) ip.getTrait(Trait.INTERNAL_LINK);
if (intLink != null) {
linkTraitFound = true;
String pvKey = intLink.getPVKey();
String idRef = intLink.getIDRef();
boolean pvKeyOK = pvKey != null && pvKey.length() > 0;
boolean idRefOK = idRef != null && idRef.length() > 0;
if (pvKeyOK && idRefOK) {
if (annotsAllowed) {
action = getPDFGoToForID(idRef, pvKey);
}
} else if (pvKeyOK) {
log.warn("Internal link trait with PageViewport key " + pvKey
+ " contains no ID reference.");
} else if (idRefOK) {
log.warn("Internal link trait with ID reference " + idRef
+ " contains no PageViewport key.");
} else {
log.warn("Internal link trait received with neither PageViewport key"
+ " nor ID reference.");
}
}
// no INTERNAL_LINK, look for EXTERNAL_LINK
if (!linkTraitFound) {
String extDest = (String) ip.getTrait(Trait.EXTERNAL_LINK);
if (extDest != null && extDest.length() > 0) {
linkTraitFound = true;
if (annotsAllowed) {
action = factory.getExternalAction(extDest);
}
}
}
// warn if link trait found but not allowed, else create link
if (linkTraitFound) {
if (!annotsAllowed) {
log.warn("Skipping annotation for a link due to PDF profile: "
+ pdfDoc.getProfile());
} else if (action != null) {
PDFLink pdfLink = factory.makeLink(ipRect, action);
currentPage.addAnnotation(pdfLink);
}
}
}