开发JavaFX需要显示pdf 的桌面应用程序。我了解到JavaFX(当前版本)不支持pdf查看/显示,我也了解到JPedal。
JavaFX
JPedal
现在,问题:
JPedalFX示例代码和用法
JPedalFX下载提供了有关使用JPedalFX的示例代码。
我有点of脚,但是我只是在这里粘贴从JPedalFX库提供的示例查看器复制的代码段示例代码。该代码依赖于JPedalFX发行版中包含的jpedal_lgpl.jar文件,该文件位于类路径(或应用程序jar清单中引用的库路径)上。
如果您对JPedalFX的使用还有其他疑问,建议您直接与IDR解决方案联系(他们过去一直在响应我)。
// 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渲染器
另外,我过去使用带有JavaFX的旧的基于SwingLabs Swing的pdf渲染器为JavaFX Web浏览器渲染pdf 。尽管在我开发浏览器时,Swing / JavaFX集成不是JavaFX的受支持功能,但对我来说仍然可以正常工作。集成代码在PDFViewer.java和BrowserWindow.java中。
需要注意的是在Swing应用程序中嵌入的JavaFX中的Java 2.2的支持和嵌入JavaFX的一个Swing应用程序中的Java 8支持。