0

フローペイン内にいくつかの分割ペインがあり、分割ペインをノードとして印刷機能に渡します。最初の分割ペインは A4 面を上にして印刷され、次のページの分割ペインは約半ページ後に印刷されます。注: 分割ペインの高さは可変です。ただし、これは、すべての分割ペインが同一である場合でも実際に発生します。

プリンタ ダイアログの明示的な A4 縦長フォーマットの設定は変更されません。シーンのサイズ変更は役に立ちません。

呼び出しシーケンスは次のとおりです。

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

ゲルハルト・ライブのサポートに感謝します

4

1 に答える 1

0

別の場所で、次のメモを見つけました。

ノードは、シーン グラフの任意の場所で最大 1 回発生する可能性があります。具体的には、ノードは、Scene のルート ノード、Parent の子 ObservableList、または Node のクリップのすべてで 1 回しか表示されない必要があります。

...

プログラムが親 (グループ、リージョンなどを含む) に子ノードを追加し、そのノードが既に別の親の子であるか、シーンのルートである場合、ノードは自動的に (そしてサイレントに) 元の親から削除されます。

...

たとえば、サブツリーをシーン グラフ内のある場所から別の場所に移動するなど、シーン グラフの構造を再配置することができます。これを行うには、通常、サブツリーを新しい場所に挿入する前に、古い場所からサブツリーを削除します。ただし、アプリケーションが明示的に削除しない場合、サブツリーは上記のように自動的に削除されます。

これは私のソリューションであり、完璧に機能します:

    @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);
    }
}
于 2015-05-20T12:55:57.020 に答える