Hello everybody,
With some hard work, I could create links on sentences of a page by
finding the location of each text sample. I've build a class over
PDFStreamEngine for that.
I've been very happy to be abble to create little boxes with a grey
border that point to page 10 of the document, but I can't find why
every box is located about 1 inch above the related text. On the other
hand, the X position is fine.
I tried to draw annotations relative to the mediaBox height (that is,
I technically mean getUpperRightY() ) and the crop box. Their is a
little decay between the both values but the annotation just won't
cover the text area.
Below is the whole testing code.
Would anybody have a clue in mind about this problem ?
Thanks a lot!
/**
* Create a link that points to page ten on every text in the current
page.
* The link has a grey border
*/
protected void processTextPosition( TextPosition text ) {
float pageHeight = getCurrentPage().findMediaBox().getUpperRightY();
log.info( text.getY()+" "+text.getX()+" "
+text.getWidth()+" "+text.getHeight()+" "+text);
try {
@SuppressWarnings("unchecked")
List<Object> annots = getCurrentPage().getAnnotations();
PDAnnotationLink link = new PDAnnotationLink();
PDGamma c = new PDGamma();
c.setB( (float) 0.5 );
c.setG( (float) 0.5 );
c.setR( (float) 0.5 );
link.setColour( c );
PDRectangle rectangle = new PDRectangle();
rectangle.setUpperRightX( text.getX() + text.getWidth() );
rectangle.setUpperRightY( pageHeight - text.getY() );
rectangle.setLowerLeftX( text.getX() );
rectangle.setLowerLeftY(
pageHeight - text.getY() + text.getHeight() );
link.setRectangle( rectangle );
PDPageXYZDestination dest = new PDPageXYZDestination();
dest.setPage( (PDPage) this.doc.getDocumentCatalog()
.getAllPages().get(10) );
dest.setTop( 0 );
link.setDestination(dest);
annots.add(link);
getCurrentPage().setAnnotations(annots);
} catch (IOException e) {
e.printStackTrace();
}
}
Julien PLÉE