Hello,
I am trying to get glyph and image positions of pdfs using pdfbox.
The thing is that with some books, that position is not the correct one (an
offset is added to that position).
I am not a pdfbox expert (I manage with the posts I find on the internet), so
may be there is something wrong in the way I get those positions.
The code to extract those positions, looks like this:
public class MyPDFRenderer extends PDFRenderer
{
...
private class MyPageDrawer extends PageDrawer
{
...
protected GlyphWrapper createGlyphWrapper( Matrix
textRenderingMatrix, PDFont font,
int
code, String unicode, Vector displacement) throws IOException
{
Shape bbox = new Rectangle2D.Float(0, 0,
font.getWidth(code) / 1000, 1);
AffineTransform at =
textRenderingMatrix.createAffineTransform();
bbox = at.createTransformedShape(bbox);
Rectangle bounds = bbox.getBounds();
bounds.y = getPageHeight() - bounds.y -
bounds.height;
GlyphWrapper result = new GlyphImpl( new int[]
{ code }, unicode, bounds );
return( result );
}
protected int getPageHeight()
{
return( (int)
getCurrentPage().getBBox().getHeight() );
}
@Override
protected void showGlyph(Matrix textRenderingMatrix,
PDFont font, int code, String unicode,
Vector
displacement) throws IOException
{
...
addGlyph( createGlyphWrapper(
textRenderingMatrix, font, code, unicode, displacement) );
...
}
@Override
public void drawImage(PDImage pdImage) throws
IOException {
super.drawImage(pdImage);
...
ImageWrapper image =
createImageWrapper( pdImage );
if( image != null )
_listOfImages.add( image );
...
}
// ami3
private Rectangle getBoundingRect() {
Matrix ctm =
getGraphicsState().getCurrentTransformationMatrix();
AffineTransform at =
ctm.createAffineTransform();
Shape bbox = new Rectangle2D.Float(0, 0, 1, 1);
bbox = at.createTransformedShape(bbox);
Rectangle result = bbox.getBounds();
result.y = getPageHeight() - result.y -
result.height;
return( result );
}
protected ImageWrapper createImageWrapper( PDImage
pdImage )
{
ImageWrapper result = null;
RenderedImage image =
ExecutionFunctions.instance().safeFunctionExecution( () -> pdImage.getImage() );
if( image != null )
{
Rectangle rect = getBoundingRect();
result = new ImageImpl( image, rect );
}
return( result );
}
...
}
...
}
And MyPDFRenderer is invoked with a dpi = 72
I have created an application with which can be seen those displacements of
glyphs and images.
You can download it from:
http://www.frojasg1.com:8080/downloads_web/downloadServletv3?file=pdfInspector.v1.0&language=English
And also a Pdf with which those displacements can be noticed:
http://www.frojasg1.com/Test-1.pdf
Does somebody know what is wrong with this way to get positions ?
How can be corrected?
Thank you
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]