use of com.sun.pdfview.PDFPage in project latexdraw by arnobl.
the class ViewText method readPDFFirstPage.
/**
* Reads and returns the first page of the given pdf document.
* @param file The file of the pdf document.
* @return The image of the first page or null.
*/
private BufferedImage readPDFFirstPage(final File file) {
BufferedImage bi = null;
try (final FileChannel fc = new RandomAccessFile(file, "r").getChannel()) {
// $NON-NLS-1$
final MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
final PDFFile pdfFile = new PDFFile(mbb);
mbb.clear();
if (pdfFile.getNumPages() == 1) {
final PDFPage page = pdfFile.getPage(0);
final Rectangle2D bound = page.getBBox();
final java.awt.Image img = page.getImage((int) bound.getWidth(), (int) bound.getHeight(), bound, null, false, true);
if (img instanceof BufferedImage) {
bi = ImageCropper.INSTANCE.cropImage((BufferedImage) img);
}
if (img != null) {
img.flush();
}
} else {
// $NON-NLS-1$
BadaboomCollector.INSTANCE.add(new IllegalArgumentException("Not a single page: " + pdfFile.getNumPages()));
}
} catch (final IOException | IllegalArgumentException | SecurityException ex) {
BadaboomCollector.INSTANCE.add(ex);
}
return bi;
}
use of com.sun.pdfview.PDFPage in project arquivoProject by fader-azevedo.
the class PdfViewerController method updateImage.
// *************** Background image loading ****************
private void updateImage(final int pageNumber) {
final Task<ImageView> updateImageTask = new Task<ImageView>() {
@Override
protected ImageView call() throws Exception {
PDFPage page = currentFile.get().getPage(pageNumber + 1);
Rectangle2D bbox = page.getBBox();
final double actualPageWidth = bbox.getWidth();
final double actualPageHeight = bbox.getHeight();
// record page dimensions for zoomToFit and zoomToWidth:
currentPageDimensions = new PageDimensions(actualPageWidth, actualPageHeight);
// width and height of image:
final int width = (int) (actualPageWidth * zoom.get());
final int height = (int) (actualPageHeight * zoom.get());
// retrieve image for page:
// width, height, clip, imageObserver, paintBackground, waitUntilLoaded:
java.awt.Image awtImage = page.getImage(width, height, bbox, null, true, true);
// draw image to buffered image:
BufferedImage buffImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
buffImage.createGraphics().drawImage(awtImage, 0, 0, null);
// convert to JavaFX image:
Image image = SwingFXUtils.toFXImage(buffImage, null);
// wrap in image view and return:
ImageView imageView = new ImageView(image);
imageView.setPreserveRatio(true);
return imageView;
}
};
updateImageTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
pagination.getScene().getRoot().setDisable(false);
currentImage.set(updateImageTask.getValue());
}
});
updateImageTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
pagination.getScene().getRoot().setDisable(false);
updateImageTask.getException().printStackTrace();
}
});
pagination.getScene().getRoot().setDisable(true);
imageLoadService.submit(updateImageTask);
}
Aggregations