public static BufferedImage getPDFPageImage(File pdfFile, int pageNumber) throws Exception {
        PDDocument document = null;
        BufferedImage pageImage = null;
        try {
            document = PDDocument.load(pdfFile);
            if (document.isEncrypted()) {
                throw new Exception("Cannot read an encrypted PDF file.");
            }
            // TODO: what is this code for?
            /*
            PDPageTree pages = document.getPages();
            PDPage page = pages.get(pageNumber - 1);
            if (page.getContents() != null) {
                PDResources resources = page.getResources();
                Iterable<COSName> names = resources.getXObjectNames();
                for (COSName name : names) {
                    PDXObject xobj = resources.getXObject(name);
                    if (xobj instanceof PDImageXObject) {
                        PDImageXObject ximage = (PDImageXObject) xobj;
                        BufferedImage bi = ximage.getImage();
                        if (bi.getType() <= 0) {
                            BufferedImage bi2 = new BufferedImage(bi.getWidth(), bi.getHeight(),
                                BufferedImage.TYPE_INT_RGB);
                            bi2.getGraphics().drawImage(bi, 0, 0, null);
                            return bi2;
                        }
                        return bi;
                    }
                }
            }
            */
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            pageImage = pdfRenderer.renderImageWithDPI(pageNumber - 1, 400);
        } catch (Exception e) {
            if (document != null) {
                document.close();
            }
            throw e;
        }
        if (document != null) {
            document.close();
        }
        return pageImage;
    }
public static BufferedImage getPDFPageImage(File pdfFile, int pageNumber) throws Exception {
        PDDocument document = null;
        BufferedImage pageImage = null;
        try {
            document = PDDocument.load(pdfFile);
            if (document.isEncrypted()) {
                throw new Exception("Cannot read an encrypted PDF file.");
            }
           
           
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            pageImage = pdfRenderer.renderImageWithDPI(pageNumber - 1, 400);
        } catch (Exception e) {
            if (document != null) {
                document.close();
            }
            throw e;
        }
        if (document != null) {
            document.close();
        }
        return pageImage;
    }
public static bufferedimage getpdfpageimage(file pdffile, int pagenumber) throws exception { pddocument document = null; bufferedimage pageimage = null; try { document = pddocument.load(pdffile); if (document.isencrypted()) { throw new exception("cannot read an encrypted pdf file."); } pdfrenderer pdfrenderer = new pdfrenderer(document); pageimage = pdfrenderer.renderimagewithdpi(pagenumber - 1, 400); } catch (exception e) { if (document != null) { document.close(); } throw e; } if (document != null) { document.close(); } return pageimage; }