1 31 package org.pdfbox.examples.util; 32 33 import org.pdfbox.cos.COSName; 34 import org.pdfbox.exceptions.InvalidPasswordException; 35 import org.pdfbox.exceptions.WrappedIOException; 36 37 import org.pdfbox.pdmodel.PDDocument; 38 import org.pdfbox.pdmodel.PDPage; 39 import org.pdfbox.pdmodel.graphics.xobject.PDXObject; 40 import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage; 41 import org.pdfbox.util.Matrix; 42 import org.pdfbox.util.PDFOperator; 43 import org.pdfbox.util.PDFStreamEngine; 44 import org.pdfbox.util.ResourceLoader; 45 46 import java.awt.geom.AffineTransform ; 47 import java.awt.geom.NoninvertibleTransformException ; 48 import java.io.IOException ; 49 50 import java.util.List ; 51 import java.util.Map ; 52 53 61 public class PrintImageLocations extends PDFStreamEngine 62 { 63 68 public PrintImageLocations() throws IOException 69 { 70 super( ResourceLoader.loadProperties( "Resources/PDFTextStripper.properties" ) ); 71 } 72 73 80 public static void main( String [] args ) throws Exception 81 { 82 if( args.length != 1 ) 83 { 84 usage(); 85 } 86 else 87 { 88 PDDocument document = null; 89 try 90 { 91 document = PDDocument.load( args[0] ); 92 if( document.isEncrypted() ) 93 { 94 try 95 { 96 document.decrypt( "" ); 97 } 98 catch( InvalidPasswordException e ) 99 { 100 System.err.println( "Error: Document is encrypted with a password." ); 101 System.exit( 1 ); 102 } 103 } 104 PrintImageLocations printer = new PrintImageLocations(); 105 List allPages = document.getDocumentCatalog().getAllPages(); 106 for( int i=0; i<allPages.size(); i++ ) 107 { 108 PDPage page = (PDPage)allPages.get( i ); 109 System.out.println( "Processing page: " + i ); 110 printer.processStream( page, page.findResources(), page.getContents().getStream() ); 111 } 112 } 113 finally 114 { 115 if( document != null ) 116 { 117 document.close(); 118 } 119 } 120 } 121 } 122 123 131 protected void processOperator( PDFOperator operator, List arguments ) throws IOException 132 { 133 String operation = operator.getOperation(); 134 if( operation.equals( "Do" ) ) 135 { 136 COSName objectName = (COSName)arguments.get( 0 ); 137 Map xobjects = getResources().getXObjects(); 138 PDXObject xobject = (PDXObject)xobjects.get( objectName.getName() ); 139 if( xobject instanceof PDXObjectImage ) 140 { 141 try 142 { 143 PDPage page = getCurrentPage(); 144 Matrix ctm = getGraphicsState().getCurrentTransformationMatrix(); 145 double rotationInRadians =(page.findRotation() * Math.PI)/180; 146 147 148 AffineTransform rotation = new AffineTransform (); 149 rotation.setToRotation( rotationInRadians ); 150 AffineTransform rotationInverse = rotation.createInverse(); 151 Matrix rotationInverseMatrix = new Matrix(); 152 rotationInverseMatrix.setFromAffineTransform( rotationInverse ); 153 Matrix rotationMatrix = new Matrix(); 154 rotationMatrix.setFromAffineTransform( rotation ); 155 156 Matrix unrotatedCTM = ctm.multiply( rotationInverseMatrix ); 157 158 System.out.println( "Found image[" + objectName.getName() + "] " + 159 "at " + unrotatedCTM.getXPosition() + "," + unrotatedCTM.getYPosition() ); 160 } 161 catch( NoninvertibleTransformException e ) 162 { 163 throw new WrappedIOException( e ); 164 } 165 } 166 } 167 else 168 { 169 super.processOperator( operator, arguments ); 170 } 171 } 172 173 176 private static void usage() 177 { 178 System.err.println( "Usage: java org.pdfbox.examples.pdmodel.PrintImageLocations <input-pdf>" ); 179 } 180 181 } | Popular Tags |