We are trying to extract a region from a Pdf and convert it to image using the PNGDevice class (see code snippet below) when the call to processToBufferedImage is called the CPU reaches 100% and takes ~25 sec to return for one region. I also tried process () and JPEGDevice all of them behave the same. Fyi we have multiple regions to extract on the same page the system stops responding when this happens.
{
Rectangle pageRect = page.getPageRect(false);
// Create PNG device with specified attributesTEST2.pdf (57.5 KB)
PngDevice pngDevice = new PngDevice();
// Convert the cropped page and save the image to stream
ByteArrayOutputStream imageOutputStream = new ByteArrayOutputStream();
BufferedImage bufferedPageImage = pngDevice.processToBufferedImage(page);
int x = (int) (zoneTextBox.getLlX() / pageRect.getWidth() * bufferedPageImage.getWidth());
int y = (int) ((pageRect.getHeight() - zoneTextBox.getUrY()) / pageRect.getHeight() * bufferedPageImage.getHeight());
int w = (int) ((zoneTextBox.getUrX() - zoneTextBox.getLlX()) / pageRect.getWidth() * bufferedPageImage.getWidth());
int h = (int) ((zoneTextBox.getUrY() - zoneTextBox.getLlY()) / pageRect.getHeight() * bufferedPageImage.getHeight());
BufferedImage croppedPageImage = bufferedPageImage.getSubimage(x, y, w, h);
try {
ImageIO.write(croppedPageImage, "png", imageOutputStream);
return imageOutputStream.toByteArray();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
imageOutputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}