1 31 package org.pdfbox; 32 33 import java.io.File ; 34 import java.io.IOException ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 39 import org.pdfbox.pdmodel.PDDocument; 40 import org.pdfbox.pdmodel.PDPage; 41 import org.pdfbox.pdmodel.PDResources; 42 import org.pdfbox.pdmodel.encryption.AccessPermission; 43 import org.pdfbox.pdmodel.encryption.StandardDecryptionMaterial; 44 import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage; 45 46 54 public class ExtractImages 55 { 56 private int imageCounter = 1; 57 58 private static final String PASSWORD = "-password"; 59 private static final String PREFIX = "-prefix"; 60 61 68 public static void main( String [] args ) throws Exception 69 { 70 ExtractImages extractor = new ExtractImages(); 71 extractor.extractImages( args ); 72 } 73 74 private void extractImages( String [] args ) throws Exception 75 { 76 if( args.length < 1 || args.length > 3 ) 77 { 78 usage(); 79 } 80 else 81 { 82 String pdfFile = null; 83 String password = ""; 84 String prefix = null; 85 for( int i=0; i<args.length; i++ ) 86 { 87 if( args[i].equals( PASSWORD ) ) 88 { 89 i++; 90 if( i >= args.length ) 91 { 92 usage(); 93 } 94 password = args[i]; 95 } 96 else if( args[i].equals( PREFIX ) ) 97 { 98 i++; 99 if( i >= args.length ) 100 { 101 usage(); 102 } 103 prefix = args[i]; 104 } 105 else 106 { 107 if( pdfFile == null ) 108 { 109 pdfFile = args[i]; 110 } 111 } 112 } 113 if(pdfFile == null) 114 { 115 usage(); 116 } 117 else 118 { 119 if( prefix == null && pdfFile.length() >4 ) 120 { 121 prefix = pdfFile.substring( 0, pdfFile.length() -4 ); 122 } 123 124 PDDocument document = null; 125 126 try 127 { 128 document = PDDocument.load( pdfFile ); 129 130 if( document.isEncrypted() ) 131 { 132 133 StandardDecryptionMaterial spm = new StandardDecryptionMaterial(password); 134 document.openProtection(spm); 135 AccessPermission ap = document.getCurrentAccessPermission(); 136 137 138 if( ! ap.canExtractContent() ) 139 { 140 throw new IOException ( 141 "Error: You do not have permission to extract images." ); 142 } 143 } 144 145 List pages = document.getDocumentCatalog().getAllPages(); 146 Iterator iter = pages.iterator(); 147 while( iter.hasNext() ) 148 { 149 PDPage page = (PDPage)iter.next(); 150 PDResources resources = page.getResources(); 151 Map images = resources.getImages(); 152 if( images != null ) 153 { 154 Iterator imageIter = images.keySet().iterator(); 155 while( imageIter.hasNext() ) 156 { 157 String key = (String )imageIter.next(); 158 PDXObjectImage image = (PDXObjectImage)images.get( key ); 159 String name = getUniqueFileName( key, image.getSuffix() ); 160 System.out.println( "Writing image:" + name ); 161 image.write2file( name ); 162 } 163 } 164 } 165 } 166 finally 167 { 168 if( document != null ) 169 { 170 document.close(); 171 } 172 } 173 } 174 } 175 } 176 177 private String getUniqueFileName( String prefix, String suffix ) 178 { 179 String uniqueName = null; 180 File f = null; 181 while( f == null || f.exists() ) 182 { 183 uniqueName = prefix + "-" + imageCounter; 184 f = new File ( uniqueName + "." + suffix ); 185 imageCounter++; 186 } 187 return uniqueName; 188 } 189 190 193 private static void usage() 194 { 195 System.err.println( "Usage: java org.pdfbox.ExtractImages [OPTIONS] <PDF file>\n" + 196 " -password <password> Password to decrypt document\n" + 197 " -prefix <image-prefix> Image prefix(default to pdf name)\n" + 198 " <PDF file> The PDF document to use\n" 199 ); 200 System.exit( 1 ); 201 } 202 203 } | Popular Tags |