JavaFX: The image gets scaled up to 25% and then printed.

I am trying to print an image using api's. Unfortunately, it reduces part of the image by about 25%, and then stretches it to the entire A4 page and prints it. What am I doing wrong with the print code. How can I specify that the image is on the page for printing, regardless of what the printer is. Please let me know.

The code:

 public void printThis() {

        System.out.println("I was called");
        // note you can use overloaded forms of the Image constructor
        // if you want to scale, etc
        String path = "resources/img/printouts/image.png";
        Image image = new Image(getClass().getResource(path).toExternalForm());
        ImageView imageView = new ImageView(image);
        new Thread(() -> printImage(imageView)).start();
    }

    public void printImage(ImageView image) {
        Printer printer = Printer.getDefaultPrinter();
        PrinterJob printJob = PrinterJob.createPrinterJob(printer);
        PageLayout pageLayout = printJob.getJobSettings().getPageLayout();
        //PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
        printJob.getJobSettings().setPageLayout(pageLayout);
        if (printJob != null) {
            boolean success = printJob.printPage(image);
            if (success) {
                printJob.endJob();
            }
        }
    }

Please let me know what I am doing wrong. Thank.: -)

0
source share
2 answers

You can add the following code to the method printImage:

image.setPreserveRatio(true);
image.setFitHeight(pageLayout.getPrintableHeight());
image.setFitWidth(pageLayout.getPrintableWidth());

, , pageLayout.getPrintableWidth() x pageLayout.getPrintableHeight(), , . ImageView.preserveRation.

0

, , .. , , ? ?

, , . , ( ). ( ), .

import javafx.application.Application;
import javafx.print.PageLayout;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;

public class ImagePrint extends Application {

    @Override
    public void start(Stage stage) {
        Image image = new Image("https://openclipart.org/image/800px/svg_to_png/93337/road08.png");
        ImageView imageView = new ImageView(image);
        new Thread(() -> printImage(imageView)).start();
    }

    public void printImage(Node node) {

        Printer printer = Printer.getDefaultPrinter();
        PageLayout pageLayout = printer.getDefaultPageLayout();
        System.out.println("PageLayout: " + pageLayout);

        // Printable area
        double pWidth = pageLayout.getPrintableWidth();
        double pHeight = pageLayout.getPrintableHeight();
        System.out.println("Printable area is " + pWidth + " width and "
                + pHeight + " height.");

        // Node (Image) dimensions
        double nWidth = node.getBoundsInParent().getWidth();
        double nHeight = node.getBoundsInParent().getHeight();
        System.out.println("Node dimensions are " + nWidth + " width and "
                + nHeight + " height");

        // How much space is left? Or is the image to big?
        double widthLeft = pWidth - nWidth;
        double heightLeft = pHeight - nHeight;
        System.out.println("Width left: " + widthLeft
                + " height left: " + heightLeft);

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

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

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

        // after scale you can check the size fit in the printable area
        double newWidth = node.getBoundsInParent().getWidth();
        double newHeight = node.getBoundsInParent().getHeight();
        System.out.println("New Node dimensions: " + newWidth
                + " width " + newHeight + " height");

        PrinterJob job = PrinterJob.createPrinterJob();
        if (job != null) {
            boolean success = job.printPage(node);
            if (success) {
                job.endJob();
                System.exit(0);
            }
        }
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
0

All Articles