{
boolean toConsole = false;
int currentArgumentIndex = 0;
String password = "";
String encoding = DEFAULT_ENCODING;
PDFTextStripper stripper = new PDFTextStripper();
String pdfFile = null;
String textFile = null;
for( int i=0; i<args.length; i++ )
{
if( args[i].equals( PASSWORD ) )
{
i++;
if( i >= args.length )
{
usage();
}
password = args[i];
}
else if( args[i].equals( ENCODING ) )
{
i++;
if( i >= args.length )
{
usage();
}
encoding = args[i];
}
else if( args[i].equals( CONSOLE ) )
{
toConsole = true;
}
else
{
if( pdfFile == null )
{
pdfFile = args[i];
}
else
{
textFile = args[i];
}
}
}
if( pdfFile == null )
{
usage();
}
if( textFile == null && pdfFile.length() >4 )
{
textFile = pdfFile.substring( 0, pdfFile.length() -4 ) + ".txt";
}
InputStream input = null;
Writer output = null;
COSDocument document = null;
try
{
input = new FileInputStream( pdfFile );
long start = System.currentTimeMillis();
document = parseDocument( input );
long stop = System.currentTimeMillis();
LOG.info( "Time to parse time=" + (stop-start) );
//document.print();
if( document.isEncrypted() )
{
try
{
DecryptDocument decryptor = new DecryptDocument( document );
decryptor.decryptDocument( password );
}
catch( InvalidPasswordException e )
{
if( args.length == 4 )//they supplied the wrong password
{
System.err.println( "Error: The supplied password is incorrect." );
System.exit( 2 );
}
else
{
//they didn't suppply a password and the default of "" was wrong.
System.err.println( "Error: The document is encrypted." );
usage();
}
}
}
if( toConsole )
{
output = new OutputStreamWriter( System.out );
}
else
{
output = new OutputStreamWriter(
new FileOutputStream( textFile ), encoding );
}
start = System.currentTimeMillis();
stripper.writeText( document, output );
stop = System.currentTimeMillis();
LOG.info( "Time to extract text time=" +(stop-start) );
}
finally
{