// 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;
                    }
                }
            }
            */
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;
    }
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);
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();
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;
}