java - 在 JavaFX 中显示 pdf

标签 java javafx

<分区>

使用 JavaFX 开发需要显示 pdf 的桌面应用程序。我了解到 JavaFX(当前版本)不支持 pdf 查看/显示,我也了解到 JPedal

现在,问题:

  1. 是否有任何外部组件或库可以在 JavaFX 中查看 pdf?它应该是一个免费软件。
  2. (如果我必须使用 JPedal)如何将它嵌入到我的应用程序中。

最佳答案

JPedalFX 示例代码和使用

使用 JPedalFX 的示例代码随 JPedalFX download 一起提供。 .

对我来说有点蹩脚,但我将在此处粘贴从 JPedalFX 库提供的示例查看器中复制的示例代码片段。该代码依赖于 JPedalFX 发行版中包含的 jpedal_lgpl.jar 文件,该文件位于类路径(或应用程序 jar list 中引用的库路径)上。

如果您对 JPedalFX 的使用有更多疑问,我建议您 contact IDR solutions directly (他们过去一直对我有回应)。

// get file path.
FileChooser fc = new FileChooser();
fc.setTitle("Open PDF file...");
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF Files", "*.pdf"));     
File f = fc.showOpenDialog(stage.getOwner());
String filename = file.getAbsolutePath();

// open file.
PdfDecoder pdf = new PdfDecoder();
pdf.openPdfFile(filename);
showPage(1);
pdf.closePdfFile();

. . . 

/**
 * Update the GUI to show a specified page.
 * @param page 
 */
private void showPage(int page) {

    //Check in range
    if (page > pdf.getPageCount())
        return;
    if (page < 1)
        return;

    //Store
    pageNumber = page;


    //Show/hide buttons as neccessary
    if (page == pdf.getPageCount())
        next.setVisible(false);
    else
        next.setVisible(true);

    if (page == 1)
        back.setVisible(false);
    else
        back.setVisible(true);


    //Calculate scale
    int pW = pdf.getPdfPageData().getCropBoxWidth(page);
    int pH = pdf.getPdfPageData().getCropBoxHeight(page);

    Dimension s = Toolkit.getDefaultToolkit().getScreenSize();

    s.width -= 100;
    s.height -= 100;

    double xScale = (double)s.width / pW;
    double yScale = (double)s.height / pH;
    double scale = xScale < yScale ? xScale : yScale;

    //Work out target size
    pW *= scale;
    pH *= scale;

    //Get image and set
    Image i = getPageAsImage(page,pW,pH);
    imageView.setImage(i);

    //Set size of components
    imageView.setFitWidth(pW);
    imageView.setFitHeight(pH);
    stage.setWidth(imageView.getFitWidth()+2);
    stage.setHeight(imageView.getFitHeight()+2);
    stage.centerOnScreen();
}

/**
 * Wrapper for usual method since JFX has no BufferedImage support.
 * @param page
 * @param width
 * @param height
 * @return 
 */
private Image getPageAsImage(int page, int width, int height) {

    BufferedImage img;
    try {
        img = pdf.getPageAsImage(page);

        //Use deprecated method since there's no real alternative 
        //(for JavaFX 2.2+ can use SwingFXUtils instead).
        if (Image.impl_isExternalFormatSupported(BufferedImage.class))
            return javafx.scene.image.Image.impl_fromExternalImage(img);

    } catch(Exception e) {
        e.printStackTrace();
    }

    return null;
}

/**
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.jpedal.org
 * (C) Copyright 1997-2008, IDRsolutions and Contributors.
 *
 *  This file is part of JPedal
 *
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 *
 * ---------------
 * JPedalFX.java
 * ---------------
 */

SwingLabs PDF 渲染器

另外,我过去使用一个旧的 SwingLabs Swing 基于 pdf 渲染器和 JavaFX 来为我的 JavaFX web browser 渲染 pdf。 .虽然在我开发浏览器时 Swing/JavaFX 集成不是 JavaFX 支持的功能,但它对我来说仍然工作得很好。集成代码在 PDFViewer.java 中和 BrowserWindow.java .

请注意 embedding JavaFX in a Swing app在 Java 2.2 和 embedding a Swing app in JavaFX 中受支持Java 8 支持。

关于java - 在 JavaFX 中显示 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18207116/

相关文章:

java - 如何使用 Spring Boot Hibernate 将 XML 字符串保存在 Postgresql 表的 XML 类型列中

java - 从另一个 Java 应用程序中查找并终止特定的 Java 进程

java - 强制关闭远程应用程序后检测Socket关闭

java - ControlsFX 的 RangeSlider 存在问题

JavaFX:从 tableview 中删除空行

javafx - 如何访问 Controller 附加到的相应节点

java - Spring 2.5 + quartz java.lang.NoClassDefFoundError : org/quartz/JobDetail

java - 如何从 JUNIT 测试用例中使用 @RequestBody 调用 Spring Controller 的方法

java - com.gluonhq.charm.glisten.control.TextField 不存在

java - 过滤列表的干净方法