我正在创建自定义的TableView,然后我需要按照它们的外观正确打印它们,所以我考虑过像AnchorPane本身的屏幕截图然后进行打印,所以这有可能吗?还是我该怎么做?
答案 0 :(得分:0)
您可以将需要打印的所有结构放置在节点中,例如anchorpane,然后将anchorPane传递给此代码,
FileChooser exportFileChooser = new FileChooser(); //The folder selector to save the pdf
exportFileChooser.setInitialDirectory(FileSystemView.getFileSystemView().getDefaultDirectory());
exportFileChooser.setInitialFileName(fileName);
File file = new File("temp/"+fileName+".png");
exportFileChooser.showSaveDialog(owner);
file.getParentFile().mkdirs();//crete temp directory if not available
final SnapshotParameters spa = new SnapshotParameters();
spa.setTransform(javafx.scene.transform.Transform.scale(0.71, 1.1));//Scaling only if required i.e., the screenshot is unable to fit in the page
WritableImage image = node.snapshot(spa, null);//This part takes the snapshot, here node is the anchorpane you sent
BufferedImage buffImage = SwingFXUtils.fromFXImage(image, null);
try {
ImageIO.write(buffImage, "png", file);//Writes the snapshot to file
} catch (Exception e) {
e.printStackTrace();
}