java - Displaying pdf in JavaFX -
developing desktop application in javafx
requires display pdf. read there no support pdf viewing/displaying in javafx
(current version), read jpedal
too.
now, questions:
- is there external component or library view pdf in javafx? should freeware.
- (if have use
jpedal
) how can embed in application.
jpedalfx sample code , usage
sample code on using jpedalfx provided jpedalfx download.
kind of lame on part, i'll paste snippets sample code here have been copied sample viewer provided jpedalfx library. code relies on jpedal_lgpl.jar file included jpedalfx distribution being on classpath (or library path referenced in manifest of application jar).
should have further questions regarding usage of jpedalfx, suggest contact idr solutions directly (they have been responsive me in past).
// 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 gui show 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 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 , set image = 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 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 , contributors. * * file part of jpedal * library free software; can redistribute and/or modify under terms of gnu lesser general public license published free software foundation; either version 2.1 of license, or (at option) later version. library distributed in hope useful, without warranty; without implied warranty of merchantability or fitness particular purpose. see gnu lesser general public license more details. should have received copy of gnu lesser general public license along library; if not, write free software foundation, inc., 59 temple place, suite 330, boston, ma 02111-1307 usa * * --------------- * jpedalfx.java * --------------- */
swinglabs pdf renderer
additionaly, used old swinglabs swing based pdf renderer javafx in past rendering pdf's javafx web browser. although swing/javafx integration wasn't supported feature of javafx @ time developed browser, still worked fine me. code integration in pdfviewer.java , browserwindow.java.
note embedding javafx in swing app supported in java 2.2 , embedding swing app in javafx supported in java 8.
Comments
Post a Comment