我们已经创建了一个程序作为单分配,并希望使用ICEpdf或其他用于Java的pdf查看器。
我们创建了不同的选项卡,用户可以在其中一个选项卡中选择一个pdf文件以“上传”并查看。问题是我们希望这样做,以便用户可以在应用程序内部查看pdf,而ICEpdf当前会创建一个额外的窗口并单独显示pdf。
可以使用ICEpdf查看器组件来实现吗?
随附一些图像以帮助解释发生了什么。
编辑/更新:基本上,ICEpdf需要一个jframe,而我们需要它在当前窗口中运行。
最佳答案
You will need to get border pane in your current window pane
/**
* 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();
}
}