Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import java.awt.image.BufferedImage;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class PDFImageViewer extends Application {
public static void main(String[] args) {
launch(args);
}
int pdfScale = 1;
@Override
public void start(Stage primaryStage) throws Exception {
int width = 590;
int height = 840;
PDDocument doc = PDDocument.load(PDFImageViewer.class.getResourceAsStream("sample.pdf"));
PDFRenderer renderer = new PDFRenderer(doc);
//PDFBoxでレンダリングして、BufferedImageを作成
BufferedImage img = renderer.renderImage(0, pdfScale);
Pane pane = new Pane();
//JavaFXで扱えるように、WritableImageに変換
WritableImage fxImage = SwingFXUtils.toFXImage(img, null);
ImageView imageView = new ImageView(fxImage);
pane.getChildren().add(imageView);
Scene scene = new Scene(pane, width, height);
primaryStage.setTitle(getClass().getName());
primaryStage.setScene(scene);
primaryStage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment