Skip to content
This repository has been archived by the owner on Aug 15, 2021. It is now read-only.

Javascript handling from java

Dansoftowner edited this page Feb 15, 2020 · 1 revision

PDFViewerFX library uses the pdf.js javascript library to read pdf documents, and just wraps it into a javaFX node.

That's why you can execute javascript code with the PDFDisplayer. For example:

   PDFDisplayer pdfDisplayer = new PDFDisplayer(new File("path/to/your/file.pdf"));
   
   //execute js
   pdfDisplayer.executeScript("document.getElementById('secondaryToolbarToggle').style.backgroundColor = 'red';");

It will produce this result:

the element turned into red color

Or

pdfDisplayer.executeScript("document.getElementById('secondaryToolbarToggle').style.backgroundColor = 'blue';");

It will produce this result:

the element turned into blue color

And of course, you can execute every other js code with it.

Catch javascript logs

This library automatically catches the javascript logs and writes it to the standard output (System.out), but if you want to change the outputstream of the js logs, or you don't want to catch these logs, you can use the JSLogListener class from the com.dansoftware.pdfdisplayer package.

For example:

   JSLogListener.setOutputStream(System.err); //setting the standard error stream for js logs
   JSLogListener.setOutputStream(null); //if we don't want to catch js logs
Clone this wiki locally