if(outputPrefix == null)
{
outputPrefix = pdfFile.substring( 0, pdfFile.lastIndexOf( '.' ));
}
PDDocument document = 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();
}
}
}
}
ImageType imageType = ImageType.RGB;
if ("bilevel".equalsIgnoreCase(color))
{
imageType = ImageType.BINARY;
}
else if ("gray".equalsIgnoreCase(color))
{
imageType = ImageType.GRAY;
}
else if ("rgb".equalsIgnoreCase(color))
{
imageType = ImageType.RGB;
}
else if ("rgba".equalsIgnoreCase(color))
{
imageType = ImageType.ARGB;
}
else
{
System.err.println( "Error: Invalid color." );
System.exit( 2 );
}
//if a CropBox has been specified, update the CropBox:
//changeCropBoxes(PDDocument document,float a, float b, float c,float d)
if ( cropBoxLowerLeftX!=0 || cropBoxLowerLeftY!=0
|| cropBoxUpperRightX!=0 || cropBoxUpperRightY!=0 )
{
changeCropBox(document,
cropBoxLowerLeftX, cropBoxLowerLeftY,
cropBoxUpperRightX, cropBoxUpperRightY);
}
// render the pages
boolean success = true;
int numPages = document.getNumberOfPages();
PDFRenderer renderer = new PDFRenderer(document);
for (int i = startPage - 1; i < endPage && i < numPages; i++)
{
BufferedImage image = renderer.renderImageWithDPI(i, dpi, imageType);
String fileName = outputPrefix + (i + 1) + "." + imageFormat;
success &= ImageIOUtil.writeImage(image, fileName, dpi);
}
if (!success)
{
System.err.println( "Error: no writer found for image format '"
+ imageFormat + "'" );
System.exit(1);
}
}
finally
{
if( document != null )
{
document.close();
}
}
}
}