Peter
Peter

Reputation: 35

Javafx-Print Job Only Print Half of the Node

I am trying to print a node from my application but only Half of the node width is printed out

Code:

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

The Node: enter image description here

The Printed Node: enter image description here

Upvotes: 0

Views: 413

Answers (1)

Matt
Matt

Reputation: 3187

Here is my Print node function it has scaling implemented already I don't remember where I pulled this from if I can find it I will link the post

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

Upvotes: 2

Related Questions