| 1 31 package org.pdfbox.examples.util; 32 33 import org.pdfbox.exceptions.InvalidPasswordException; 34 35 36 import org.pdfbox.pdmodel.PDDocument; 37 import org.pdfbox.pdmodel.PDPage; 38 import org.pdfbox.util.PDFTextStripper; 39 import org.pdfbox.util.TextPosition; 40 41 import java.io.IOException ; 42 43 import java.util.List ; 44 45 53 public class PrintTextLocations extends PDFTextStripper 54 { 55 60 public PrintTextLocations() throws IOException  61 { 62 super.setSortByPosition( true ); 63 } 64 65 72 public static void main( String [] args ) throws Exception  73 { 74 if( args.length != 1 ) 75 { 76 usage(); 77 } 78 else 79 { 80 PDDocument document = null; 81 try 82 { 83 document = PDDocument.load( args[0] ); 84 if( document.isEncrypted() ) 85 { 86 try 87 { 88 document.decrypt( "" ); 89 } 90 catch( InvalidPasswordException e ) 91 { 92 System.err.println( "Error: Document is encrypted with a password." ); 93 System.exit( 1 ); 94 } 95 } 96 PrintTextLocations printer = new PrintTextLocations(); 97 List allPages = document.getDocumentCatalog().getAllPages(); 98 for( int i=0; i<allPages.size(); i++ ) 99 { 100 PDPage page = (PDPage)allPages.get( i ); 101 System.out.println( "Processing page: " + i ); 102 printer.processStream( page, page.findResources(), page.getContents().getStream() ); 103 } 104 } 105 finally 106 { 107 if( document != null ) 108 { 109 document.close(); 110 } 111 } 112 } 113 } 114 115 121 protected void showCharacter( TextPosition text ) 122 { 123 System.out.println( "String[" + text.getX() + "," + 124 text.getY() + " fs=" + text.getFontSize() + " xscale=" + 125 text.getXScale() + " height=" + text.getHeight() + " width=" + 126 text.getWidth() + "]" + text.getCharacter() ); 127 } 128 129 132 private static void usage() 133 { 134 System.err.println( "Usage: java org.pdfbox.examples.pdmodel.PrintTextLocations <input-pdf>" ); 135 } 136 137 } | Popular Tags |