在流程窗格中,我有几个拆分窗格并将拆分窗格作为节点移交给打印功能。第一个拆分窗格打印在 A4 面朝上,大约半页后在后续页面上打印任何拆分窗格。注意:拆分窗格具有可变高度。但是,即使所有拆分窗格都相同,这仍然有效。
设置打印机对话框显式 A4 纵向格式,不会更改。场景调整大小没有帮助。
调用顺序如下:
容器控制器.java
@FXML
private void handlePrint(ActionEvent event) {
try {
FXPrinter fxPrinter = new FXPrinter();
for (Object split : flowPane.getChildren()) {
Node node = null;
node = (Node) split;
fxPrinter.printFX(getPrimaryStage(), node);
}
} catch (IOException ex) {
Logger.getLogger(ContainerController.class.getName()).log(Level.SEVERE, null, ex);
}
}
公共类 FXPrinter { ...
public void printFX(Stage primaryStage, Node node) {
Printer printer = Printer.getDefaultPrinter();
PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
double scaleX = pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();
double scaleY = pageLayout.getPrintableHeight() / node.getBoundsInParent().getHeight();
PrinterJob job = PrinterJob.createPrinterJob(printer);
job.getJobSettings().setPageLayout(pageLayout);
if (scaleY < 1.0) {
// more then one page
if (scaleY < scaleX) {
// more than one page, we must take Y stretchfactor
node.getTransforms().add(new Scale(scaleX, scaleY));
} else {
// take X stretchfactor for Y for proportional both, X/Y stretching
node.getTransforms().add(new Scale(scaleX, scaleX));
}
} else {
// less then one page, set x proportional to y, equal stretchfactor
node.getTransforms().add(new Scale(scaleX, scaleX));
}
if (job != null) {
if (job.showPrintDialog(primaryStage.getOwner()) && job.printPage(pageLayout, node)) {
job.endJob();
}
}
}
永远感谢您的支持 Gerhard Laib