Javafx-Print作业仅打印一半节点

时间:2018-10-14 20:33:23

标签: java javafx printing

我正在尝试从应用程序中打印一个节点,但仅打印出一半的节点宽度

代码:

@FXML
private void print() {
    PrinterJob job = PrinterJob.createPrinterJob();
        if (job != null && job.showPrintDialog(stackPane.getScene().getWindow())){
            boolean success = job.printPage(stackPane);
            if (success) {
                job.endJob();
            }
        }
}

节点: enter image description here

打印节点: enter image description here

1 个答案:

答案 0 :(得分:0)

这是我的Print节点函数,它已经实现了缩放功能,我不记得我从哪里提取了它,如果我能找到它,我将链接该帖子

private void printImage(Node node) {
    Printer printer = Printer.getDefaultPrinter();
    PageLayout pageLayout = printer.getDefaultPageLayout();

    // Printable area
    double pWidth = pageLayout.getPrintableWidth();
    double pHeight = pageLayout.getPrintableHeight();

    // Node's (Image) dimensions
    double nWidth = node.getBoundsInParent().getWidth();
    double nHeight = node.getBoundsInParent().getHeight();

    // How much space is left? Or is the image to big?
    double widthLeft = pWidth - nWidth;
    double heightLeft = pHeight - nHeight;

    // scale the image to fit the page in width, height or both
    double scale;

    if (widthLeft < heightLeft) scale = pWidth / nWidth;
    else scale = pHeight / nHeight;

    // preserve ratio (both values are the same)
    node.getTransforms().add(new Scale(scale, scale));

    PrinterJob job = PrinterJob.createPrinterJob();
    if (job != null) {
        boolean success = job.printPage(node);
        if (success) {
            System.out.println("PRINTING FINISHED");
            job.endJob();
        }
    }
}