简体   繁体   中英

Printing SplitPanes within a FlowPane prints from the second page half page down

Within a flowpane I have several splitpanes and hand over the splitpanes as node to the print function. The first splitpane is printed on the A4 side up, any more splitpane on the following pages by approximately half a page later. Note: the splitpane have a variable height. However, this occurs in effect even if all splitpanes are identical.

Setting the printer dialog explicit A4 portrait format, does not change. The Scene Resize does not help.

The calling sequence is as follows:

ContainerController.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);
    }
}

public class 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();
        }
    }
}

Ever grateful for your Support Gerhard Laib

Elsewhere I found the following note:

A node may occur at most once anywhere in the scene graph. Specifically, a node must appear no more than once in all of the following: as the root node of a Scene, the children ObservableList of a Parent, or as the clip of a Node.

...

If a program adds a child node to a Parent (including Group, Region, etc) and that node is already a child of a different Parent or the root of a Scene, the node is automatically (and silently) removed from its former parent.

...

It is possible to rearrange the structure of the scene graph, for example, to move a subtree from one location in the scene graph to another. In order to do this, one would normally remove the subtree from its old location before inserting it at the new location. However, the subtree will be automatically removed as described above if the application doesn't explicitly remove it.

This is my solution and it works perfect:

    @FXML
private void handlePrint(ActionEvent event) {
    try {
        FXPrinter fxPrinter = new FXPrinter();
        int size = flowPane.getChildren().size();
        for (int i = 0; i < size; i++) {
            Node node = (Node) flowPane.getChildren().get(0);
            fxPrinter.print(getPrimaryStage(), node);
            flowPane.getChildren().remove(0);
        }
        // reload 
        loadSplitPanes();
    } catch (IOException ex) {
        Logger.getLogger(ContainerController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM