Author: lehmi
Date: Mon Dec 26 16:09:15 2011
New Revision: 1224747
URL: http://svn.apache.org/viewvc?rev=1224747&view=rev
Log:
PDFBOX-585, PDFBOX-1197: fixed the calculation of the image size, added
processing of XImageForms to get all images of a pdf
Modified:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintImageLocations.java
Modified:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintImageLocations.java
URL:
http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintImageLocations.java?rev=1224747&r1=1224746&r2=1224747&view=diff
==============================================================================
---
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintImageLocations.java
(original)
+++
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintImageLocations.java
Mon Dec 26 16:09:15 2011
@@ -17,12 +17,16 @@
package org.apache.pdfbox.examples.util;
import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.exceptions.InvalidPasswordException;
import org.apache.pdfbox.exceptions.WrappedIOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.graphics.PDGraphicsState;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
import org.apache.pdfbox.util.Matrix;
import org.apache.pdfbox.util.PDFOperator;
@@ -46,6 +50,8 @@ import java.util.Map;
*/
public class PrintImageLocations extends PDFStreamEngine
{
+
+ private static final String INVOKE_OPERATOR = "Do";
/**
* Default constructor.
*
@@ -118,42 +124,80 @@ public class PrintImageLocations extends
protected void processOperator( PDFOperator operator, List arguments )
throws IOException
{
String operation = operator.getOperation();
- if( operation.equals( "Do" ) )
+ if( INVOKE_OPERATOR.equals(operation) )
{
COSName objectName = (COSName)arguments.get( 0 );
- Map xobjects = getResources().getXObjects();
+ Map<String, PDXObject> xobjects = getResources().getXObjects();
PDXObject xobject = (PDXObject)xobjects.get( objectName.getName()
);
if( xobject instanceof PDXObjectImage )
{
- try
+ PDXObjectImage image = (PDXObjectImage)xobject;
+ PDPage page = getCurrentPage();
+ int imageWidth = image.getWidth();
+ int imageHeight = image.getHeight();
+ double pageHeight = page.getMediaBox().getHeight();
+
System.out.println("*******************************************************************");
+ System.out.println("Found image [" + objectName.getName() +
"]");
+
+ Matrix ctmNew =
getGraphicsState().getCurrentTransformationMatrix();
+ float yScaling = ctmNew.getYScale();
+ float angle = (float)Math.acos(ctmNew.getValue(0,
0)/ctmNew.getXScale());
+ if (ctmNew.getValue(0, 1) < 0 && ctmNew.getValue(1, 0) > 0)
{
- PDXObjectImage image = (PDXObjectImage)xobject;
- PDPage page = getCurrentPage();
- Matrix ctm =
getGraphicsState().getCurrentTransformationMatrix();
- double rotationInRadians =(page.findRotation() *
Math.PI)/180;
-
-
- AffineTransform rotation = new AffineTransform();
- rotation.setToRotation( rotationInRadians );
- AffineTransform rotationInverse = rotation.createInverse();
- Matrix rotationInverseMatrix = new Matrix();
- rotationInverseMatrix.setFromAffineTransform(
rotationInverse );
- Matrix rotationMatrix = new Matrix();
- rotationMatrix.setFromAffineTransform( rotation );
-
- Matrix unrotatedCTM = ctm.multiply( rotationInverseMatrix
);
- float xScale = unrotatedCTM.getXScale();
- float yScale = unrotatedCTM.getYScale();
-
- System.out.println( "Found image[" + objectName.getName()
+ "] " +
- "at " + unrotatedCTM.getXPosition() + "," +
unrotatedCTM.getYPosition() +
- " size=" + (xScale/100f*image.getWidth()) + "," +
(yScale/100f*image.getHeight() ));
+ angle = (-1)*angle;
+ }
+ ctmNew.setValue(2, 1, (float)(pageHeight -
ctmNew.getYPosition() - Math.cos(angle)*yScaling));
+ ctmNew.setValue(2, 0, (float)(ctmNew.getXPosition() -
Math.sin(angle)*yScaling));
+ // because of the moved 0,0-reference, we have to shear in the
opposite direction
+ ctmNew.setValue(0, 1, (-1)*ctmNew.getValue(0, 1));
+ ctmNew.setValue(1, 0, (-1)*ctmNew.getValue(1, 0));
+ AffineTransform ctmAT = ctmNew.createAffineTransform();
+ ctmAT.scale(1f/imageWidth, 1f/imageHeight);
+
+ float imageXScale = ctmNew.getXScale();
+ float imageYScale = ctmNew.getYScale();
+ System.out.println("position = " + ctmNew.getXPosition() + ",
" + ctmNew.getYPosition());
+ // size in pixel
+ System.out.println("size = " + imageWidth + "px, " +
imageHeight + "px");
+ // size in page units
+ System.out.println("size = " + imageXScale + ", " +
imageYScale);
+ // size in inches
+ imageXScale /= 72;
+ imageYScale /= 72;
+ System.out.println("size = " + imageXScale + "in, " +
imageYScale + "in");
+ // size in millimeter
+ imageXScale *= 25.4;
+ imageYScale *= 25.4;
+ System.out.println("size = " + imageXScale + "mm, " +
imageYScale + "mm");
+ System.out.println();
+ }
+ else if(xobject instanceof PDXObjectForm)
+ {
+ // save the graphics state
+ getGraphicsStack().push(
(PDGraphicsState)getGraphicsState().clone() );
+ PDPage page = getCurrentPage();
+
+ PDXObjectForm form = (PDXObjectForm)xobject;
+ COSStream invoke = (COSStream)form.getCOSObject();
+ PDResources pdResources = form.getResources();
+ if(pdResources == null)
+ {
+ pdResources = page.findResources();
}
- catch( NoninvertibleTransformException e )
+ // if there is an optional form matrix, we have to
+ // map the form space to the user space
+ Matrix matrix = form.getMatrix();
+ if (matrix != null)
{
- throw new WrappedIOException( e );
+ Matrix xobjectCTM = matrix.multiply(
getGraphicsState().getCurrentTransformationMatrix());
+
getGraphicsState().setCurrentTransformationMatrix(xobjectCTM);
}
+ processSubStream( page, pdResources, invoke );
+
+ // restore the graphics state
+ setGraphicsState( (PDGraphicsState)getGraphicsStack().pop() );
}
+
}
else
{