KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > util > PrintImageLocations


1 /**
2  * Copyright (c) 2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

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 JavaDoc;
47 import java.awt.geom.NoninvertibleTransformException JavaDoc;
48 import java.io.IOException JavaDoc;
49
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52
53 /**
54  * This is an example on how to get the x/y coordinates of image locations.
55  *
56  * Usage: java org.pdfbox.examples.util.PrintImageLocations <input-pdf>
57  *
58  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
59  * @version $Revision: 1.3 $
60  */

61 public class PrintImageLocations extends PDFStreamEngine
62 {
63     /**
64      * Default constructor.
65      *
66      * @throws IOException If there is an error loading text stripper properties.
67      */

68     public PrintImageLocations() throws IOException
69     {
70         super( ResourceLoader.loadProperties( "Resources/PDFTextStripper.properties" ) );
71     }
72     
73     /**
74      * This will print the documents data.
75      *
76      * @param args The command line arguments.
77      *
78      * @throws Exception If there is an error parsing the document.
79      */

80     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
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 JavaDoc 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     /**
124      * This is used to handle an operation.
125      *
126      * @param operator The operation to perform.
127      * @param arguments The list of arguments.
128      *
129      * @throws IOException If there is an error processing the operation.
130      */

131     protected void processOperator( PDFOperator operator, List JavaDoc arguments ) throws IOException
132     {
133         String JavaDoc operation = operator.getOperation();
134         if( operation.equals( "Do" ) )
135         {
136             COSName objectName = (COSName)arguments.get( 0 );
137             Map JavaDoc 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 JavaDoc rotation = new AffineTransform JavaDoc();
149                     rotation.setToRotation( rotationInRadians );
150                     AffineTransform JavaDoc 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 JavaDoc e )
162                 {
163                     throw new WrappedIOException( e );
164                 }
165             }
166         }
167         else
168         {
169             super.processOperator( operator, arguments );
170         }
171     }
172
173     /**
174      * This will print the usage for this document.
175      */

176     private static void usage()
177     {
178         System.err.println( "Usage: java org.pdfbox.examples.pdmodel.PrintImageLocations <input-pdf>" );
179     }
180
181 }
Popular Tags