You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Fran Rojas <fr...@gmail.com> on 2020/05/05 09:36:58 UTC

Glyphs and images with an offset only with some pdf books

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: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Glyphs and images with an offset only with some pdf books

Posted by Fran Rojas <fr...@gmail.com>.
Thank you very much.

There are examples of everything, very userful.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Glyphs and images with an offset only with some pdf books

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,

take a look at the DrawPrintTextLocations example in the examples package. Thsi will give you some hints of hot to get the glyph
positions.

BR
Maruan
 
> 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: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org