gpt4 book ai didi

JavaFX : display pdf with icepdf

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:38 24 4
gpt4 key购买 nike

我正在寻找一种在我的 JavaFx 应用程序中显示 pdf 文件的方法。我正在尝试使用 icePDF 来做到这一点,但我似乎只能找到 swing 的代码。现在我将代码插入到 SwingNode 并将其嵌入到 Pane 中,但问题是包含 icePDF pdf 阅读器的 JPanel 不适合 JavaFx Pane 的大小。

我是这样做的:

private void handleApercuBtn(){
SwingController controller = new SwingController();

SwingViewBuilder factory = new SwingViewBuilder(controller);

JPanel viewerComponentPanel = factory.buildViewerPanel();

ComponentKeyBinding.install(controller, viewerComponentPanel);

controller.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController()));

final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode,viewerComponentPanel);

apercuPane.getChildren().add(swingNode);

controller.openDocument(filePath);
}

如何将包含 icePDF 阅读器的 JPanel 放入我的面板 (apercuPane)?我怎么能不使用 Swing 代码来执行此操作?

最佳答案

/**
* FXML Controller class
*
* @author Sudarshan
*/
public class PDFFXMLController implements Initializable {
private static String pdfPath;
private SwingController swingController;
private JComponent viewerPanel;
@FXML
private BorderPane borderPane;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
String pdfPathLoad;
try {
pdfPathLoad = loadPDF("http://21-04-2017/17854381660C617.pdf");
createViewer(borderPane);
openDocument(pdfPathLoad );
} catch (IOException ex) {
Logger.getLogger(PDFFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void createViewer(BorderPane borderPane) {
try {
SwingUtilities.invokeAndWait(() -> {
swingController = new SwingController();
swingController.setIsEmbeddedComponent(true);
PropertiesManager properties = new PropertiesManager(System.getProperties(),
ResourceBundle.getBundle(PropertiesManager.DEFAULT_MESSAGE_BUNDLE));
properties.set(PropertiesManager.PROPERTY_SHOW_TOOLBAR_FIT, "false");
properties.set(PropertiesManager.PROPERTY_SHOW_TOOLBAR_ROTATE, "false");
properties.set(PropertiesManager.PROPERTY_SHOW_TOOLBAR_TOOL, "false");
properties.set(PropertiesManager.PROPERTY_DEFAULT_ZOOM_LEVEL, "1.25");
properties.setBoolean(PropertiesManager.PROPERTY_SHOW_STATUSBAR_VIEWMODE, Boolean.FALSE);
properties.set(PropertiesManager.PROPERTY_SHOW_TOOLBAR_PAGENAV, "false");
ResourceBundle messageBundle = ResourceBundle.getBundle(PropertiesManager.DEFAULT_MESSAGE_BUNDLE);
new FontPropertiesManager(properties, System.getProperties(), messageBundle);
swingController.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(swingController.getDocumentViewController()));
SwingViewBuilder factory = new SwingViewBuilder(swingController, properties);
viewerPanel = factory.buildViewerPanel();
viewerPanel.revalidate();
SwingNode swingNode = new SwingNode();
swingNode.setContent(viewerPanel);
borderPane.setCenter(swingNode);
swingController.setToolBarVisible(false);
swingController.setUtilityPaneVisible(false);
});
} catch (InterruptedException | InvocationTargetException e) {
}
}
private void openDocument(String document) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
swingController.openDocument(document);
viewerPanel.revalidate();
}
});
}
public String loadPDF(String adresse) throws IOException {
System.out.println("In load PDf");
if (!adresse.toLowerCase().endsWith("pdf")) {
return null;
}
String fileName = adresse.substring(adresse.lastIndexOf("/") + 1,
adresse.lastIndexOf("."));
String suffix = adresse.substring(adresse.lastIndexOf("."),
adresse.length());
File temp = null;
try {
InputStream input = new URL(adresse).openStream();
temp = File.createTempFile(fileName, suffix);
temp.deleteOnExit();
Files.copy(input, Paths.get(temp.toURI()),
StandardCopyOption.REPLACE_EXISTING);
} catch (MalformedURLException ex) {
Logger.getLogger(PDFFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
return temp.getAbsolutePath();
}

}

关于JavaFX : display pdf with icepdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32611666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com