Home » pdfbox-1.1.0-src » org.apache.pdfbox.examples.util » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.pdfbox.examples.util;
   18   
   19   import org.apache.pdfbox.cos.COSName;
   20   import org.apache.pdfbox.exceptions.InvalidPasswordException;
   21   import org.apache.pdfbox.exceptions.WrappedIOException;
   22   
   23   import org.apache.pdfbox.pdmodel.PDDocument;
   24   import org.apache.pdfbox.pdmodel.PDPage;
   25   import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
   26   import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
   27   import org.apache.pdfbox.util.Matrix;
   28   import org.apache.pdfbox.util.PDFOperator;
   29   import org.apache.pdfbox.util.PDFStreamEngine;
   30   import org.apache.pdfbox.util.ResourceLoader;
   31   
   32   import java.awt.geom.AffineTransform;
   33   import java.awt.geom.NoninvertibleTransformException;
   34   import java.io.IOException;
   35   
   36   import java.util.List;
   37   import java.util.Map;
   38   
   39   /**
   40    * This is an example on how to get the x/y coordinates of image locations.
   41    *
   42    * Usage: java org.apache.pdfbox.examples.util.PrintImageLocations <input-pdf>
   43    *
   44    * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
   45    * @version $Revision: 1.5 $
   46    */
   47   public class PrintImageLocations extends PDFStreamEngine
   48   {
   49       /**
   50        * Default constructor.
   51        *
   52        * @throws IOException If there is an error loading text stripper properties.
   53        */
   54       public PrintImageLocations() throws IOException
   55       {
   56           super( ResourceLoader.loadProperties( "Resources/PDFTextStripper.properties", true ) );
   57       }
   58   
   59       /**
   60        * This will print the documents data.
   61        *
   62        * @param args The command line arguments.
   63        *
   64        * @throws Exception If there is an error parsing the document.
   65        */
   66       public static void main( String[] args ) throws Exception
   67       {
   68           if( args.length != 1 )
   69           {
   70               usage();
   71           }
   72           else
   73           {
   74               PDDocument document = null;
   75               try
   76               {
   77                   document = PDDocument.load( args[0] );
   78                   if( document.isEncrypted() )
   79                   {
   80                       try
   81                       {
   82                           document.decrypt( "" );
   83                       }
   84                       catch( InvalidPasswordException e )
   85                       {
   86                           System.err.println( "Error: Document is encrypted with a password." );
   87                           System.exit( 1 );
   88                       }
   89                   }
   90                   PrintImageLocations printer = new PrintImageLocations();
   91                   List allPages = document.getDocumentCatalog().getAllPages();
   92                   for( int i=0; i<allPages.size(); i++ )
   93                   {
   94                       PDPage page = (PDPage)allPages.get( i );
   95                       System.out.println( "Processing page: " + i );
   96                       printer.processStream( page, page.findResources(), page.getContents().getStream() );
   97                   }
   98               }
   99               finally
  100               {
  101                   if( document != null )
  102                   {
  103                       document.close();
  104                   }
  105               }
  106           }
  107       }
  108   
  109       /**
  110        * This is used to handle an operation.
  111        *
  112        * @param operator The operation to perform.
  113        * @param arguments The list of arguments.
  114        *
  115        * @throws IOException If there is an error processing the operation.
  116        */
  117       protected void processOperator( PDFOperator operator, List arguments ) throws IOException
  118       {
  119           String operation = operator.getOperation();
  120           if( operation.equals( "Do" ) )
  121           {
  122               COSName objectName = (COSName)arguments.get( 0 );
  123               Map xobjects = getResources().getXObjects();
  124               PDXObject xobject = (PDXObject)xobjects.get( objectName.getName() );
  125               if( xobject instanceof PDXObjectImage )
  126               {
  127                   try
  128                   {
  129                       PDXObjectImage image = (PDXObjectImage)xobject;
  130                       PDPage page = getCurrentPage();
  131                       Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
  132                       double rotationInRadians =(page.findRotation() * Math.PI)/180;
  133   
  134   
  135                       AffineTransform rotation = new AffineTransform();
  136                       rotation.setToRotation( rotationInRadians );
  137                       AffineTransform rotationInverse = rotation.createInverse();
  138                       Matrix rotationInverseMatrix = new Matrix();
  139                       rotationInverseMatrix.setFromAffineTransform( rotationInverse );
  140                       Matrix rotationMatrix = new Matrix();
  141                       rotationMatrix.setFromAffineTransform( rotation );
  142   
  143                       Matrix unrotatedCTM = ctm.multiply( rotationInverseMatrix );
  144                       float xScale = unrotatedCTM.getXScale();
  145                       float yScale = unrotatedCTM.getYScale();
  146   
  147                       System.out.println( "Found image[" + objectName.getName() + "] " +
  148                               "at " + unrotatedCTM.getXPosition() + "," + unrotatedCTM.getYPosition() +
  149                               " size=" + (xScale/100f*image.getWidth()) + "," + (yScale/100f*image.getHeight() ));
  150                   }
  151                   catch( NoninvertibleTransformException e )
  152                   {
  153                       throw new WrappedIOException( e );
  154                   }
  155               }
  156           }
  157           else
  158           {
  159               super.processOperator( operator, arguments );
  160           }
  161       }
  162   
  163       /**
  164        * This will print the usage for this document.
  165        */
  166       private static void usage()
  167       {
  168           System.err.println( "Usage: java org.apache.pdfbox.examples.pdmodel.PrintImageLocations <input-pdf>" );
  169       }
  170   
  171   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.examples.util » [javadoc | source]