如何将打印作业传递给javafx applcation中的特定打印机?

我正在申请一个应用程序,这个应用程序是在javafx,在这个应用程序中我们正在接受食品订单和这个订单我们必须使用不同的打印机打印,一些打印机将在厨房的一些总部。 在我的系统中,我需要打印机列表,当我从我的应用程序中按下打印按钮时,我将从列表中选择打印机。 因此打印作业将传递给选定的打印机。如何在我的javafx应用程序中完成此操作?

我使用以下方法,但它将printjob传递给默认打印机,该打印机由系统选择而不是由应用程序选择: –

public void print(Node node) { Printer printer = Printer.getDefaultPrinter(); PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT); double scaleX = node.getBoundsInParent().getWidth(); double scaleY = node.getBoundsInParent().getHeight(); node.getTransforms().add(new Scale(scaleX, scaleY)); PrinterJob job = PrinterJob.createPrinterJob(); if (job != null) { boolean success = job.printPage(node); if (success) { job.endJob(); } } } 

这是如何通过打印机打印作业,但没有从打印机打印:

 ChoiceDialog dialog = new ChoiceDialog(Printer.getDefaultPrinter(), Printer.getAllPrinters()); //ChoiceDialog dialog = new ChoiceDialog(printerName1, printerName2, printerName3, printerName4, printerName5); dialog.setHeaderText("Choose the printer!"); dialog.setContentText("Choose a printer from available printers"); dialog.setTitle("Printer Choice"); Optional opt = dialog.showAndWait(); if (opt.isPresent()) { Printer printer = opt.get(); PrinterJob job = PrinterJob.createPrinterJob(); job.setPrinter(printer); if (job != null) { boolean success = job.printPage(node); if (success) { job.endJob(); } } } 

您可以使用ChoiceDialogPrinter.getAllPrinters返回的打印机Set选择Printer

 ChoiceDialog dialog = new ChoiceDialog(Printer.getDefaultPrinter(), Printer.getAllPrinters()); dialog.setHeaderText("Choose the printer!"); dialog.setContentText("Choose a printer from available printers"); dialog.setTitle("Printer Choice"); Optional opt = dialog.showAndWait(); if (opt.isPresent()) { Printer printer = opt.get(); // start printing ... } 

当然,如果您不想使用对话框,也可以使用任何其他方式从项目列表中选择单个项目。 例如

  • ListView
  • ComboBox
  • TableView

顺便说一句:节点的大小将为0,除非它们是布局的,这可能会导致

 double scaleX = node.getBoundsInParent().getWidth(); double scaleY = node.getBoundsInParent().getHeight(); node.getTransforms().add(new Scale(scaleX, scaleY)); 

将其缩放为0 。 对于尚未显示的节点,您需要自己进行布局(请参阅以下答案: https : //stackoverflow.com/a/26152904/2991525 ):

 Group g = new Group(node); Scene scene = new Scene(g); g.applyCss(); g.layout(); double scaleX = node.getBoundsInParent().getWidth(); double scaleY = node.getBoundsInParent().getHeight(); 

但是我不确定你想要通过缩放实现什么…节点越大,缩放因子就越不合理,特别是如果高度和宽度不同的话。