Below is a sample code to integrate our PDF viewing, annotating and form filling component, jPDFNotesBean, into a JavaFX application.

This sample shows 2 ways of integrating the Java bean:

  • Frame: Display Qoppa’s PDF bean Inside a JFrame. This works well.
  • Embedded: Display Qoppa’s PDF bean inside a Swing Node within a JavaFX Scene. There are a few issues with this approach including some layout issues, the annotation properties dialog being shown behind and the popup not displaying when trying to show annotations’ notes, in addition to some cursor issues. But it is overall functional.

This sample can be adapted to integrate our PDF Viewing-Only component PDFViewerBean or our advanced PDF editing component PDFEditorBean by simply replacing PDFNotesBean with PDFViewerBean or PDFEDitorBean in the code below.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
 
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
 
import javax.swing.*;
 
import com.qoppa.pdf.PDFException;
import com.qoppa.pdfNotes.PDFNotesBean;
 
public class JavaFXNotes extends Application {
 
    private ObjectProperty pdf = new SimpleObjectProperty<>();
 
    public static void main(String[] args) {
        Application.launch(args);
    }
 
    @Override
    public void start(Stage primaryStage) throws Exception {
        Button btnEmbedded = new Button("Embedded");
        btnEmbedded.disableProperty().bind(pdf.isNull());
        btnEmbedded.setOnAction(event2 -> openEmbedded());
        Button btnJframe = new Button("JFrame");
        btnJframe.setOnAction(event1 -> openJFrame());
        btnJframe.disableProperty().bind(pdf.isNull());
        Button pickFile = new Button("Pick file");
 
        pickFile.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("PDF", ".pdf"));
            pdf.set(fileChooser.showOpenDialog(pickFile.getScene().getWindow()));
        });
 
        Scene primaryScene = new Scene(new VBox(pickFile, new HBox(10, btnEmbedded, btnJframe)));
        primaryStage.setScene(primaryScene);
        primaryStage.show();
    }
 
    private void openEmbedded() {
        Stage embeddedStage = new Stage();
        SwingNode sw = new SwingNode();
        sw.setContent(createAndLoad());
        Scene embeddedScene = new Scene(new StackPane(sw), 500, 500);
        embeddedStage.setScene(embeddedScene);
        embeddedStage.show();
    }
 
    private void openJFrame() {
        JFrame jframe = new JFrame();
        jframe.setContentPane(createAndLoad());
        jframe.setSize(500, 500);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
 
    public PDFNotesBean createAndLoad() {
        long before = System.currentTimeMillis();
        PDFNotesBean notesBean = new PDFNotesBean();
        System.out.println("After: " + (System.currentTimeMillis() - before));
        new Thread(() -> {
            try {
                notesBean.loadPDF(new FileInputStream(pdf.get()));
            } catch (PDFException | FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        }).run();
        return notesBean;
    }
 
}

Thank you to the customer who wrote and shared this sample code with us.

Download JavaFXNotes Sample Code

Download jPDFNotes.jar

Tagged: