I'm trying to make viewing pdf-files using java. I understand that the standard tools do not work out and found jPDFViewerFX.jar from Qoppa Software, but apparently it is not free because there is a "company label" on all the pages diagonally

The source code is implemented simply

PDFViewer pdfViewer = new PDFViewer(); pdfViewer.setSplitVisible(false); pdfViewer.setToolBarVisible(false); pdfViewer.loadPDF("C:\\pdffile.pdf"); anchpPDFReadder.getChildren().add(pdfViewer); 

Are there any other free components (libraries) for viewing pdf-files. Preferably with an example.

  • A similar question with the answer (and an example) on the English-language version of the forum: Displaying pdf in JavaFX - Alexander Savostyanov
  • Thank you for your response. I almost solved this task using the org.apache.pdfbox component (library) and the Pagination component. As soon as I finish, I will immediately share my method - p_redator

1 answer 1

I solved my problem from the source https://github.com/torutk/pdfviewer/tree/master/src/pdfviewer with the help of org.apache.pdfbox and Pagination.

Main.java

 import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml")); Parent root = loader.load(); Controller controller = loader.getController(); controller.start(); primaryStage.setTitle("Pdffile"); primaryStage.setScene(new Scene(root)); primaryStage.show(); } public static void main(String[] args) { launch(args); } } 

PdfModel.java

 import javafx.embed.swing.SwingFXUtils; import javafx.scene.image.Image; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Path; public class PdfModel { private PDDocument document; private PDFRenderer renderer; PdfModel(Path path) { try { document = PDDocument.load(path.toFile()); renderer = new PDFRenderer(document); } catch (IOException ex) { throw new UncheckedIOException("PDDocument thorws IOException file=" + path, ex); } } int numPages() { return document.getPages().getCount(); } Image getImage(int pageNumber) { BufferedImage pageImage; try { pageImage = renderer.renderImage(pageNumber); } catch (IOException ex) { throw new UncheckedIOException("PDFRenderer throws IOException", ex); } return SwingFXUtils.toFXImage(pageImage, null); } } 

Controller.java

 import javafx.fxml.FXML; import javafx.scene.control.Pagination; import javafx.scene.image.ImageView; import java.nio.file.Paths; public class Controller { @FXML private Pagination pagination; private PdfModel pdfModel; public void start(){ pdfModel = new PdfModel(Paths.get("c:\\pdffile.pdf")); pagination.setPageCount(pdfModel.numPages()); pagination.setPageFactory(index -> new ImageView(pdfModel.getImage(index))); } }