Skip to content

Instantly share code, notes, and snippets.

@c9katayama
Last active December 16, 2015 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c9katayama/bad980d6bfaf5150281d to your computer and use it in GitHub Desktop.
Save c9katayama/bad980d6bfaf5150281d to your computer and use it in GitHub Desktop.
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