Решил свою задачу из источника https://github.com/torutk/pdfviewer/tree/master/src/pdfviewer при помощи org.apache.pdfbox и 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)));
}
}