如何将打印作业传递给javafx applcation中的特定打印机? [英] How pass a print job to a specific printer in javafx applcation?

查看:101
本文介绍了如何将打印作业传递给javafx applcation中的特定打印机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  public void print(节点节点){
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(成功){
job.endJob();
}
}
}

这是我通过打印机的方式打印作业,但没有从打印机打印:

  ChoiceDialog对话框=新的ChoiceDialog(Printer.getDefaultPrinter(),打印机。 getAllPrinters()); 
// ChoiceDialog对话框=新的ChoiceDialog(printerName1,printerName2,printerName3,printerName4,printerName5);
dialog.setHeaderText(选择打印机!);
dialog.setContentText(从可用打印机中选择打印机);
dialog.setTitle(打印机选择);
可选<打印机> 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(成功){
job.endJob();
}
}
}


解决方案

您可以使用 ChoiceDialog 为此目的从 Set 打印机 c> <$返回的打印机c $ c> Printer.getAllPrinters

  ChoiceDialog对话框=新的ChoiceDialog(打印机) .getDefaultPrinter(),Printer.getAllPrinters()); 
dialog.setHeaderText(选择打印机!);
dialog.setContentText(从可用打印机中选择打印机);
dialog.setTitle(打印机选择);
可选<打印机> opt = dialog.showAndWait();
if(opt.isPresent()){
Printer printer = opt.get();
//开始打印...
}

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




  • ListView

  • ComboBox

  • TableView



BTW:节点的大小将为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 ):

 组g =新组(节点); 
场景场景=新场景(g);
g.applyCss();
g.layout();
double scaleX = node.getBoundsInParent()。getWidth();
double scaleY = node.getBoundsInParent()。getHeight();

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


am working on an application , this application is in javafx, in this application we are taking food orders and this order we have to print using different printer, some printer will be in the kitchen some in the head office. In my system i need list of printers and when i press print button from my application that time i will select printer from the list. So the print job will passed to the selected printer.How i will done this in my javafx application?

Am using this following method but it pass printjob to default printer which is selected by the system not by the applicaiton:-

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

This is how am passing printer to print job, but not getting print from the printer:

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

解决方案

You can use a ChoiceDialog for that purpose to choose a Printer from the Set of printers returned by Printer.getAllPrinters:

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<Printer> opt = dialog.showAndWait();
if (opt.isPresent()) {
    Printer printer = opt.get();
    // start printing ...
}

Of course you could use any other way to choose a single item from a list of items too, if you prefer not to use a dialog. E.g.

  • ListView
  • ComboBox
  • TableView

BTW: the size of nodes will be 0, unless they were layouted, which could cause

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

to scale it to 0. For nodes not already displayed, you need to layout them yourself (see this answer: 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();

But I'm not sure what you're trying to achieve with the scaling anyway... The larger the node, the greater the scaling factor is not really a reasonable thing to do, especially if the height and width differ.

这篇关于如何将打印作业传递给javafx applcation中的特定打印机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆