use of com.sun.pdfview.PDFFile 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.PDFFile in project latexdraw by arnobl.
the class ViewText method createImage.
/**
* @return The LaTeX compiled picture of the text with its file path and its log.
*/
private Tuple<Image, String> createImage() {
final Optional<File> optDir = LFileUtils.INSTANCE.createTempDir();
if (!optDir.isPresent()) {
return new Tuple<>(null, "A temporary file cannot be created.");
}
BufferedImage bi = null;
// $NON-NLS-1$
String log = "";
final File tmpDir = optDir.get();
final String doc = getLaTeXDocument();
// $NON-NLS-1$
final String basePathPic = tmpDir.getAbsolutePath() + LSystem.FILE_SEP + "latexdrawTmpPic" + System.currentTimeMillis();
final String pathTex = basePathPic + ExportFormat.TEX.getFileExtension();
final OperatingSystem os = LSystem.INSTANCE.getSystem().orElse(OperatingSystem.LINUX);
// Saving the LaTeX document into a file to be compiled.
if (!LFileUtils.INSTANCE.saveFile(pathTex, doc).isPresent()) {
return new Triple<>(null, basePathPic, log);
}
// Compiling the LaTeX document.
Tuple<Boolean, String> res = execute(new String[] { // $NON-NLS-1$ //$NON-NLS-2$
os.getLatexBinPath(), // $NON-NLS-1$ //$NON-NLS-2$
"--halt-on-error", // $NON-NLS-1$ //$NON-NLS-2$
"--interaction=nonstopmode", "--output-directory=" + tmpDir.getAbsolutePath(), // $NON-NLS-1$
LFileUtils.INSTANCE.normalizeForLaTeX(pathTex) });
boolean ok = res.a;
log = res.b;
// Compiling the DVI document.
if (ok) {
// $NON-NLS-1$ //$NON-NLS-2$
res = execute(new String[] { os.getDvipsBinPath(), basePathPic + ".dvi", "-o", basePathPic + ExportFormat.EPS_LATEX.getFileExtension() });
ok = res.a;
log = log + res.b;
}
// Converting the PS document as a PDF one.
if (ok) {
res = execute(new String[] { os.getPs2pdfBinPath(), basePathPic + ExportFormat.EPS_LATEX.getFileExtension(), basePathPic + ExportFormat.PDF.getFileExtension() });
ok = res.a;
log = log + res.b;
}
// Getting the image of the first page of the PDF document.
if (ok) {
final String pdfpath = basePathPic + ExportFormat.PDF.getFileExtension();
final File pdfFile = new File(pdfpath);
bi = readPDFFirstPage(pdfFile);
}
// Converting the image as a JFX one.
final Image fxImage;
if (bi == null) {
fxImage = null;
} else {
fxImage = SwingFXUtils.toFXImage(bi, null);
bi.flush();
}
// Deleting the temporary folder and its content.
LFileUtils.INSTANCE.removeDirWithContent(tmpDir.getPath());
return new Tuple<>(fxImage, log);
}
use of com.sun.pdfview.PDFFile in project arquivoProject by fader-azevedo.
the class PdfViewerController method bindPaginationToCurrentFile.
private void bindPaginationToCurrentFile() {
currentFile.addListener(new ChangeListener<PDFFile>() {
@Override
public void changed(ObservableValue<? extends PDFFile> observable, PDFFile oldFile, PDFFile newFile) {
if (newFile != null) {
pagination.setCurrentPageIndex(0);
}
}
});
pagination.pageCountProperty().bind(new IntegerBinding() {
{
super.bind(currentFile);
}
@Override
protected int computeValue() {
return currentFile.get() == null ? 0 : currentFile.get().getNumPages();
}
});
pagination.disableProperty().bind(Bindings.isNull(currentFile));
}
use of com.sun.pdfview.PDFFile in project arquivoProject by fader-azevedo.
the class PdfViewerController method loadFile.
public void loadFile(String caminho) {
File file = new File(caminho);
if (file != null) {
final Task<PDFFile> loadFileTask = new Task<PDFFile>() {
@Override
protected PDFFile call() throws Exception {
try (RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel()) {
ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
return new PDFFile(buffer);
}
}
};
loadFileTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
pagination.getScene().getRoot().setDisable(false);
final PDFFile pdfFile = loadFileTask.getValue();
currentFile.set(pdfFile);
}
});
loadFileTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
pagination.getScene().getRoot().setDisable(false);
showErrorMessage("Não é possivel ler " + file.getName(), loadFileTask.getException());
btnZoomIn.setDisable(true);
btnZoomOut.setDisable(true);
}
});
imageLoadService.submit(loadFileTask);
}
}
Aggregations