1 31 package org.pdfbox; 32 33 import java.awt.image.BufferedImage ; 34 import java.io.File ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 import javax.imageio.IIOException ; 39 import javax.imageio.IIOImage ; 40 import javax.imageio.ImageIO ; 41 import javax.imageio.ImageWriteParam ; 42 import javax.imageio.ImageWriter ; 43 import javax.imageio.stream.ImageOutputStream ; 44 45 import org.pdfbox.exceptions.InvalidPasswordException; 46 47 import org.pdfbox.pdmodel.PDDocument; 48 import org.pdfbox.pdmodel.PDPage; 49 50 56 public class PDFToImage 57 { 58 59 private static final String PASSWORD = "-password"; 60 private static final String START_PAGE = "-startPage"; 61 private static final String END_PAGE = "-endPage"; 62 private static final String IMAGE_TYPE = "-imageType"; 63 private static final String OUTPUT_PREFIX = "-outputPrefix"; 64 65 68 private PDFToImage() 69 { 70 } 72 73 80 public static void main( String [] args ) throws Exception 81 { 82 String password = ""; 83 String pdfFile = null; 84 String outputPrefix = null; 85 String imageType = "jpg"; 86 int startPage = 1; 87 int endPage = Integer.MAX_VALUE; 88 for( int i=0; i<args.length; i++ ) 89 { 90 if( args[i].equals( PASSWORD ) ) 91 { 92 i++; 93 if( i >= args.length ) 94 { 95 usage(); 96 } 97 password = args[i]; 98 } 99 else if( args[i].equals( START_PAGE ) ) 100 { 101 i++; 102 if( i >= args.length ) 103 { 104 usage(); 105 } 106 startPage = Integer.parseInt( args[i] ); 107 } 108 else if( args[i].equals( END_PAGE ) ) 109 { 110 i++; 111 if( i >= args.length ) 112 { 113 usage(); 114 } 115 endPage = Integer.parseInt( args[i] ); 116 } 117 else if( args[i].equals( IMAGE_TYPE ) ) 118 { 119 i++; 120 imageType = args[i]; 121 } 122 else if( args[i].equals( OUTPUT_PREFIX ) ) 123 { 124 i++; 125 outputPrefix = args[i]; 126 } 127 else 128 { 129 if( pdfFile == null ) 130 { 131 pdfFile = args[i]; 132 } 133 } 134 } 135 136 if( pdfFile == null ) 137 { 138 usage(); 139 } 140 else 141 { 142 if(outputPrefix == null) 143 { 144 outputPrefix = pdfFile.substring( 0, pdfFile.lastIndexOf( '.' )); 145 } 146 147 PDDocument document = null; 148 try 149 { 150 document = PDDocument.load( pdfFile ); 151 152 153 if( document.isEncrypted() ) 155 { 156 try 157 { 158 document.decrypt( password ); 159 } 160 catch( InvalidPasswordException e ) 161 { 162 if( args.length == 4 ) { 164 System.err.println( "Error: The supplied password is incorrect." ); 165 System.exit( 2 ); 166 } 167 else 168 { 169 System.err.println( "Error: The document is encrypted." ); 171 usage(); 172 } 173 } 174 } 175 List pages = document.getDocumentCatalog().getAllPages(); 176 for( int i=startPage-1; i<endPage && i<pages.size(); i++ ) 177 { 178 ImageOutputStream output = null; 179 ImageWriter imageWriter = null; 180 try 181 { 182 PDPage page = (PDPage)pages.get( i ); 183 BufferedImage image = page.convertToImage(); 184 String fileName = outputPrefix + (i+1) + "." + imageType; 185 System.out.println( "Writing:" + fileName ); 186 output = ImageIO.createImageOutputStream( new File ( fileName ) ); 187 188 boolean foundWriter = false; 189 Iterator writerIter = ImageIO.getImageWritersByFormatName( imageType ); 190 while( writerIter.hasNext() && !foundWriter ) 191 { 192 try 193 { 194 imageWriter = (ImageWriter )writerIter.next(); 195 ImageWriteParam writerParams = imageWriter.getDefaultWriteParam(); 196 if(writerParams.canWriteCompressed() ) 197 { 198 writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 199 writerParams.setCompressionQuality(1.0f); 200 } 201 202 203 imageWriter.setOutput( output ); 204 imageWriter.write( null, new IIOImage ( image, null, null), writerParams ); 205 foundWriter = true; 206 } 207 catch( IIOException io ) 208 { 209 } 211 finally 212 { 213 if( imageWriter != null ) 214 { 215 imageWriter.dispose(); 216 } 217 } 218 } 219 if( !foundWriter ) 220 { 221 throw new RuntimeException ( "Error: no writer found for image type '" + imageType + "'" ); 222 } 223 } 224 finally 225 { 226 if( output != null ) 227 { 228 output.flush(); 229 output.close(); 230 } 231 } 232 } 233 } 234 finally 235 { 236 if( document != null ) 237 { 238 document.close(); 239 } 240 } 241 } 242 } 243 244 247 private static void usage() 248 { 249 System.err.println( "Usage: java org.pdfbox.PDFToImage [OPTIONS] <PDF file>\n" + 250 " -password <password> Password to decrypt document\n" + 251 " -imageType <image type> (" + getImageFormats() + ")\n" + 252 " -outputPrefix <output prefix> Filename prefix for image files\n" + 253 " -startPage <number> The first page to start extraction(1 based)\n" + 254 " -endPage <number> The last page to extract(inclusive)\n" + 255 " <PDF file> The PDF document to use\n" 256 ); 257 System.exit( 1 ); 258 } 259 260 private static String getImageFormats() 261 { 262 StringBuffer retval = new StringBuffer (); 263 String [] formats = ImageIO.getReaderFormatNames(); 264 for( int i=0; i<formats.length; i++ ) 265 { 266 retval.append( formats[i] ); 267 if( i+1<formats.length ) 268 { 269 retval.append( "," ); 270 } 271 } 272 return retval.toString(); 273 } 274 } | Popular Tags |