String password = "";
String split = null;
String startPage = null;
String endPage = null;
boolean useNonSeqParser = false;
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( args[i].equals( START_PAGE ) )
{
i++;
if( i >= args.length )
{
usage();
}
startPage = args[i];
}
else if( args[i].equals( END_PAGE ) )
{
i++;
if( i >= args.length )
{
usage();
}
endPage = args[i];
}
else if( args[i].equals( NONSEQ ) )
{
useNonSeqParser = true;
}
else
{
if( pdfFile == null )
{
pdfFile = args[i];
}
}
}
if( pdfFile == null )
{
usage();
}
else
{
PDDocument document = null;
List<PDDocument> documents = null;
try
{
if (useNonSeqParser)
{
document = PDDocument.loadNonSeq(new File(pdfFile), password);
}
else
{
document = PDDocument.load(pdfFile);
if( document.isEncrypted() )
{
try
{
StandardDecryptionMaterial sdm = new StandardDecryptionMaterial(password);
document.openProtection(sdm);
}
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 supply a password and the default of "" was wrong.
System.err.println( "Error: The document is encrypted." );
usage();
}
}
}
}
int numberOfPages = document.getNumberOfPages();
boolean startEndPageSet = false;
if (startPage != null)
{
splitter.setStartPage(Integer.parseInt( startPage ));
startEndPageSet = true;
if (split == null)
{
splitter.setSplitAtPage(numberOfPages);
}
}
if (endPage != null)
{
splitter.setEndPage(Integer.parseInt( endPage ));
startEndPageSet = true;
if (split == null)
{
splitter.setSplitAtPage(Integer.parseInt( endPage ));
}
}
if (split != null)
{
splitter.setSplitAtPage( Integer.parseInt( split ) );
}
else
{
if (!startEndPageSet)
{
splitter.setSplitAtPage(1);
}
}
documents = splitter.split( document );
for( int i=0; i<documents.size(); i++ )
{
PDDocument doc = documents.get( i );
String fileName = pdfFile.substring(0, pdfFile.length()-4 ) + "-" + i + ".pdf";
writeDocument( doc, fileName );