IGraphicsContext graphics = null;
PDDocument pdfDoc = null;
if (inputFile != null && inputFile.exists() && inputFile.isFile()) {
try {
pdfDoc = openDoc(inputFile, password);
PDPage pdPage = pdfDoc.getPageTree().getPageAt(page - 1);
Rectangle2D rect = pdPage.getCropBox().toNormalizedRectangle();
retVal = new BufferedImage((int) rect.getWidth(), (int) rect.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) retVal.getGraphics();
graphics = new CwtAwtGraphicsContext(g2);
// setup user space
AffineTransform imgTransform = graphics.getTransform();
imgTransform.scale(1, -1);
imgTransform.translate(-rect.getMinX(), -rect.getMaxY());
graphics.setTransform(imgTransform);
graphics.setBackgroundColor(Color.WHITE);
graphics.fill(rect);
CSContent content = pdPage.getContentStream();
if (content != null) {
JPodRenderer renderer = new JPodRenderer(null, graphics);
renderer.process(content, pdPage.getResources());
}
if (pdfDoc != null) {
pdfDoc.close();
}
int totalRotation = (rotation + pdPage.getRotate()) % 360;
if (totalRotation != 0) {
Image rotated = ImageUtility.rotateImage(retVal, totalRotation);
retVal = new BufferedImage(rotated.getWidth(null), rotated.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics g = retVal.getGraphics();