private void split( String[] args ) throws Exception
{
String password = "";
String split = "1";
Splitter splitter = new Splitter();
String pdfFile = 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( SPLIT ) )
{
i++;
if( i >= args.length )
{
usage();
}
split = args[i];
}
else
{
if( pdfFile == null )
{
pdfFile = args[i];
}
}
}
if( pdfFile == null )
{
usage();
}
else
{
InputStream input = null;
PDDocument document = null;
List documents = null;
try
{
input = new FileInputStream( pdfFile );
document = parseDocument( input );
if( document.isEncrypted() )
{
try
{
document.decrypt( 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();
}
}
}
splitter.setSplitAtPage( Integer.parseInt( split ) );
documents = splitter.split( document );
for( int i=0; i<documents.size(); i++ )
{
PDDocument doc = (PDDocument)documents.get( i );
String fileName = pdfFile.substring(0, pdfFile.length()-4 ) + "-" + i + ".pdf";
writeDocument( doc, fileName );