java swing 显示pdf,在JavaFX中显示pdf

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:

Is there any external component or library to view pdf in JavaFX? It should be a freeware.

(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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值