在JavaFX中显示pdf [英] Displaying pdf in JavaFX

查看:2261
本文介绍了在JavaFX中显示pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



现在,问题:


  1. 是否有任何外部组件或库可供查看pdf在JavaFX中?它应该是一个免费软件。

  2. (如果我必须使用 JPedal )如何将其嵌入我的应用程序中。


解决方案

JPedalFX示例代码和用法



使用JPedalFX的示例代码随 JPedalFX下载一起提供。



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



你应该关于JPedalFX的使用还有其他问题,我建议你直接联系IDR解决方案(他们过去一直对我做出回应)。

  //获取文件路径。 
FileChooser fc = new FileChooser();
fc.setTitle(打开PDF文件...);
fc.getExtensionFilters()。add(new FileChooser.ExtensionFilter(PDF Files,* .pdf));
File f = fc.showOpenDialog(stage.getOwner());
String filename = file.getAbsolutePath();

//打开文件。
PdfDecoder pdf = new PdfDecoder();
pdf.openPdfFile(filename);
showPage(1);
pdf.closePdfFile();

。 。 。

/ **
*更新GUI以显示指定页面。
* @param page
* /
private void showPage(int page){

//签入范围
if(page> pdf。 getPageCount())
return;
if(page< 1)
return;

//存储
pageNumber = page;


//显示/隐藏按钮为必要
if(page == pdf.getPageCount())
next.setVisible(false);
else
next.setVisible(true);

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


//计算比例
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;

//计算出目标规模
pW * = scale;
pH * =比例;

//获取图像并设置
图像i = getPageAsImage(页面,pW,pH);
imageView.setImage(i);

//设置组件大小
imageView.setFitWidth(pW);
imageView.setFitHeight(pH);
stage.setWidth(imageView.getFitWidth()+ 2);
stage.setHeight(imageView.getFitHeight()+ 2);
stage.centerOnScreen();
}

/ **
*常用方法的包装器,因为JFX没有BufferedImage支持。
* @param page
* @param width
* @param height
* @return
* /
private Image getPageAsImage(int page,int width ,int height){

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

//使用不推荐的方法,因为没有真正的替代方法
//(对于JavaFX 2.2+,可以使用SwingFXUtils代替)。
if(Image.impl_isExternalFormatSupported(BufferedImage.class))
返回javafx.scene.image.Image.impl_fromExternalImage(img);

} catch(例外e){
e.printStackTrace();
}

返回null;
}

/ **
* ============================= ==============
* Java Pdf提取解码访问库
* ==================== =======================
*
*项目信息:http://www.jpedal.org
* (C)版权所有1997-2008,IDRsolutions and Contributors。
*
*此文件是JPedal的一部分
*
此库是免费软件;您可以根据自由软件基金会发布的GNU Lesser General Public
许可条款对其进行重新分发和/或
进行修改;许可证的
版本2.1,或(根据您的选择)任何更高版本。

这个库的发布是希望它有用,
但没有任何担保;甚至没有暗示的保证b $ b b适销性或特定用途的适用性。有关更多详细信息,请参阅GNU
较宽松通用公共许可证。

您应该已经收到了GNU Lesser General Public
许可证以及该库的副本;如果没有,请写信给自由软件
Foundation,Inc.,59 Temple Place,Suite 330,Boston,MA 02111-1307 USA


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

SwingLabs PDF渲染器



<另外,我过去使用旧的基于SwingLabs Swing的pdf渲染器和JavaFX渲染pdf用于我的 JavaFX Web浏览器。虽然Swing / JavaFX集成在我开发浏览器时并不是JavaFX的支持功能,但它仍然适用于我。集成代码位于 PDFViewer.java BrowserWindow.java



请注意在Swing应用程序中嵌入JavaFX 在JavaFX中嵌入Swing应用程序在Java 8中支持


Developing a desktop application in JavaFX which requires to display a pdf. I read that there is no support for pdf viewing/displaying in JavaFX(current version), I read about JPedal too.

Now, questions:

  1. Is there any external component or library to view pdf in JavaFX? It should be a freeware.
  2. (If I have to use JPedal) How can I embed it in my application.

解决方案

JPedalFX Sample Code and Usage

Sample code on using JPedalFX is provided with the JPedalFX download.

Kind of lame on my part, but I'll just paste snippets sample code here that have been copied from the sample viewer provided with the JPedalFX library. The code relies on the jpedal_lgpl.jar file included with the JPedalFX distribution being on the classpath (or the library path referenced in the manifest of your application jar).

Should you have further questions regarding usage of JPedalFX, I suggest you contact IDR solutions directly (they have been responsive to me in the past).

// 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 Renderer

Additionaly, I used an old SwingLabs Swing based pdf renderer with JavaFX in the past for rendering pdf's for my JavaFX web browser. Although the Swing/JavaFX integration wasn't a supported feature of JavaFX at the time that I developed the browser, it still worked fine for me. Code for integration is in PDFViewer.java and BrowserWindow.java.

Note that embedding JavaFX in a Swing app is supported in Java 2.2 and embedding a Swing app in JavaFX is supported in Java 8.

这篇关于在JavaFX中显示pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆