Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Launch a Swing Based PDF Viewer</strong></p> <p>Here is a <a href="http://code.google.com/p/willow-browser/source/browse/src/main/java/org/jewelsea/willow/BrowserWindow.java" rel="nofollow">code extract</a> from a small web browser (named the <a href="http://code.google.com/p/willow-browser/" rel="nofollow">willow-browser</a>) I wrote in JavaFX:</p> <pre><code>webEngine.locationProperty().addListener(new ChangeListener&lt;String&gt;() { @Override public void changed(ObservableValue&lt;? extends String&gt; observableValue, String oldLoc, String newLoc) { if (newLoc.endsWith(".pdf")) { try { final PDFViewer pdfViewer = new PDFViewer(false); pdfViewer.openFile(new URL(newLoc)); } catch (Exception ex) { // handle bad pdf url . . . most likely no action required } } } } </code></pre> <p>The code makes use of a 3rd party Swing based pdf viewer which it launches to load the url. As the Swing pdf viewer runs in a seperate window, there were not any Swing/JavaFX integration issues around it - it just seemed to work.</p> <p>To get it to work, you can download a copy of the willow browser source and build it (the sample binary hosted on the site doesn't include the pdf viewer, only the source builds do).</p> <p>Ideally, the code would query the http content type in addition to the uri string to determine that the file is a pdf, but that requires a bit of low level network access which is difficult to get at in the case of WebView. If you are not concerned about being perfectly accurate at detecting pdf content on many sites, it won't really matter if you don't make use of the content type to determine pdfs. </p> <p>There are other pdf viewers around which you could try if the Swing pdf viewer doesn't provide you with the experience you want, e.g. <a href="http://www.idrsolutions.com/jpedalfx-viewer/" rel="nofollow">jpedalfx</a> or <a href="http://www.icesoft.org/java/demos/icepdf-demos.jsf" rel="nofollow">ICEpdf</a>.</p> <p><strong>Launch a Native PDF Viewer</strong></p> <p>If you prefer you could call <a href="http://docs.oracle.com/javafx/2/api/javafx/application/HostServices.html#showDocument%28java.lang.String%29" rel="nofollow">HostServices.showDocument(uri)</a> on the pdf url and that will trigger to open the url in a local web browser, some of which have built in pdf viewers or are setup to view the pdf using the Acrobat reader.</p> <p>The code to determine the url for the pdf to view is the same in this case as the code above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload