Extract Image from PDF using Java -
i need extract bar-code pdf (using rectangle), not converting whole pdf image.
the image format can jpg/png.
with pdf box, without coding:
"$java_home/bin/java" -jar pdfbox-app-1.8.2.jar pdftoimage foo.pdf
to batch processing:
import java.io.file; import java.io.filenamefilter; import java.util.arrays; import java.util.list; import java.util.observer; import org.apache.pdfbox.pdftoimage; public class main { static { system.setproperty( "org.apache.commons.logging.log", "org.apache.commons.logging.impl.nooplog" ); } public static int extract( list< file > files, file jpegdir, observer observer ) { jpegdir.mkdirs(); int done = 0; for( final file file : files ) { try { final file target = new file( jpegdir, file.getname()); final string trgtpath = target.getpath(); final string prefix = trgtpath.substring( 0, trgtpath.lastindexof( '.' )); pdftoimage.main( new string[]{ "-outputprefix", prefix, file.getpath() }); final double percent = (100.0 * ++done ) / files.size(); system.out.printf( "%6.2f %%: %s\n", percent, file.getname()); if( observer != null ) { observer.update( null, file ); } } catch( final throwable t ) { system.err.println( file.getpath()); t.printstacktrace(); } } return done; } public static void main( string[] args ) { if( args.length != 2 ) { system.err.println( "usage: java -jar pdf2img.jar <pdf directory> <jpeg directory>" ); system.exit(1); } final file pdfdir = new file( args[0] ); final file jpegdir = new file( args[1] ); final file[] files = pdfdir.listfiles( new filenamefilter() { @override public boolean accept( file dir, string name ) { return name.tolowercase().endswith( ".pdf" ); }}); if( files != null ) { final int done = extract( arrays.aslist( files ), jpegdir, null ); system.out.printf( "\n%d file%s processed.", done, ( done > 1 ) ? "s" : "" ); } } }
this utility may associated gui (french localized):
public final class gui extends application { @override public void start( stage primarystage ) throws exception { final borderpane pane = new borderpane(); final hbox toppane = new hbox(); final label lbl = new label( "répertoire des images : " ); final textfield jpegdir = new textfield(); final button browsebtn = new button( "parcourir..." ); final tableview< file > filesvw = new tableview<>(); lbl.setalignment( pos.center_left ); lbl .setstyle( "-fx-padding:8px; -fx-margin:8px;" ); jpegdir .setstyle( "-fx-padding:8px; -fx-margin:8px;" ); browsebtn.setstyle( "-fx-padding:8px; -fx-margin:8px;" ); toppane.getchildren().addall( lbl, jpegdir, browsebtn ); pane.settop( toppane ); pane.setcenter( filesvw ); jpegdir.setprefcolumncount( 40 ); jpegdir.seteditable( false ); final observablelist< tablecolumn< file, ? >> columns = filesvw.getcolumns(); final tablecolumn< file, string > name = new tablecolumn<>( "nom" ); name.setcellvaluefactory( new callback< celldatafeatures< file, string >, observablevalue< string >>(){ @override public observablevalue< string > call( celldatafeatures< file, string > p ){ return new simplestringproperty( p.getvalue().getname()); }}); name.setsortable( false ); name.setprefwidth( 400 ); columns.add( name ); final tablecolumn< file, string > size = new tablecolumn<>( "taille" ); size.setcellvaluefactory( new callback< celldatafeatures< file, string >, observablevalue< string >>(){ @override public observablevalue< string > call( celldatafeatures< file, string > p ){ return new simplestringproperty( string.format( "%,12d", p.getvalue().length())); }}); size.setsortable( true ); size.setprefwidth( 80 ); columns.add( size ); final tablecolumn< file, string > date = new tablecolumn<>( "date" ); final simpledateformat sdf = new simpledateformat( "dd/mm/yyyy hh:mm" ); date.setcellvaluefactory( new callback< celldatafeatures< file, string >, observablevalue< string >>(){ @override public observablevalue< string > call( celldatafeatures< file, string > p ){ return new simplestringproperty( sdf.format( new date( p.getvalue().lastmodified()))); }}); date.setsortable( true ); date.setprefwidth( 120 ); columns.add( date ); final map< file, simplebooleanproperty > dones = new hashmap<>(); final tablecolumn< file, boolean > done = new tablecolumn<>( "traité" ); done.setcellvaluefactory( new callback< celldatafeatures< file, boolean >, observablevalue< boolean >>(){ @override public observablevalue< boolean > call( celldatafeatures< file, boolean > p ){ return dones.get( p.getvalue()); }}); done.setcellfactory( new callback<tablecolumn<file,boolean>,tablecell<file,boolean>>(){ @override public tablecell<file,boolean> call( tablecolumn<file,boolean> p ){ return new checkboxtablecell<>(); }}); done.setsortable( true ); done.setprefwidth( 40 ); columns.add( done ); jpegdir.setondragover(new eventhandler <dragevent>() { @override public void handle(dragevent event) { if (event.getgesturesource() != jpegdir ) { event.accepttransfermodes(transfermode.copy_or_move); } event.consume(); }}); jpegdir.setondragdropped(new eventhandler <dragevent>() { @override public void handle(dragevent event) { final dragboard db = event.getdragboard(); boolean success = false; if( db.hasfiles()) { jpegdir.settext( db.getfiles().get( 0 ).getpath()); success = true; } event.setdropcompleted( success ); event.consume(); }}); filesvw.setondragover(new eventhandler <dragevent>() { @override public void handle(dragevent event) { if( event.getgesturesource() != filesvw && ! jpegdir.gettext().isempty()) { event.accepttransfermodes(transfermode.copy_or_move); } event.consume(); }}); filesvw.setondragdropped(new eventhandler <dragevent>() { @override public void handle(dragevent event) { final dragboard db = event.getdragboard(); boolean success = false; if( db.hasfiles()) { final list< file > files = db.getfiles(); final file target = new file( jpegdir.gettext()); for( final file f : files ) { dones.put( f, new simplebooleanproperty( false )); } filesvw.getitems().addall( files ); filesvw.setdisable( true ); new thread(){@override public void run() { main.extract( files, target, new observer(){ @override public void update( observable o, final object file ) { platform.runlater( new runnable() { @override public void run() { dones.get( file ).setvalue( boolean.true ); }}); }}); platform.runlater( new runnable() { @override public void run() { filesvw.setdisable( false ); }}); }}.start(); success = true; } event.setdropcompleted( success ); event.consume(); }}); primarystage.setscene( new scene( pane )); primarystage.setx( 0 ); primarystage.sety( 0 ); primarystage.show(); } public static void main( string[] args ) { launch(); } }