Search in sources :

Example 1 with PDFFile

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;
}
Also used : FileChannel(java.nio.channels.FileChannel) Rectangle2D(java.awt.geom.Rectangle2D) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) PDFFile(com.sun.pdfview.PDFFile) PDFPage(com.sun.pdfview.PDFPage)

Example 2 with PDFFile

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);
}
Also used : OperatingSystem(net.sf.latexdraw.util.OperatingSystem) Triple(net.sf.latexdraw.util.Triple) BufferedImage(java.awt.image.BufferedImage) Image(javafx.scene.image.Image) RandomAccessFile(java.io.RandomAccessFile) PDFFile(com.sun.pdfview.PDFFile) File(java.io.File) Tuple(net.sf.latexdraw.util.Tuple) BufferedImage(java.awt.image.BufferedImage)

Example 3 with PDFFile

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));
}
Also used : PDFFile(com.sun.pdfview.PDFFile) IntegerBinding(javafx.beans.binding.IntegerBinding)

Example 4 with PDFFile

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);
    }
}
Also used : Task(javafx.concurrent.Task) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) PDFFile(com.sun.pdfview.PDFFile) WorkerStateEvent(javafx.concurrent.WorkerStateEvent) RandomAccessFile(java.io.RandomAccessFile) PDFFile(com.sun.pdfview.PDFFile) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Aggregations

PDFFile (com.sun.pdfview.PDFFile)4 RandomAccessFile (java.io.RandomAccessFile)3 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2 FileChannel (java.nio.channels.FileChannel)2 PDFPage (com.sun.pdfview.PDFPage)1 Rectangle2D (java.awt.geom.Rectangle2D)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 IntegerBinding (javafx.beans.binding.IntegerBinding)1 Task (javafx.concurrent.Task)1 WorkerStateEvent (javafx.concurrent.WorkerStateEvent)1 Image (javafx.scene.image.Image)1 OperatingSystem (net.sf.latexdraw.util.OperatingSystem)1 Triple (net.sf.latexdraw.util.Triple)1 Tuple (net.sf.latexdraw.util.Tuple)1