简体   繁体   English

如何在JavaFX中打印窗格?

[英]How to print Pane in JavaFX?

How can I print my Pane that has Label inside of it? 如何打印其中带有Label Pane I want to print it using my POS Printer 55mm. 我想使用55mm的POS打印机进行打印。 I have this kind of code to print, but it prints nothing to me: 我有要打印的这种代码,但对我却什么也没打印:

void print2(Node node){
    PrinterJob job = PrinterJob.createPrinterJob();
    Printer printer = Printer.getDefaultPrinter().getDefaultPrinter();
    PageLayout pageLayout = printer.createPageLayout(Paper.A6, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
    JobSettings jobSettings = job.getJobSettings();
    jobSettings.setPageLayout(pageLayout);
    boolean printed = job.printPage(node);
    if (printed) {
        job.endJob();
    }
}

The code does not look bad. 代码看起来不错。 There are a few points I would recommend to check out: 我建议您检查以下几点:

  • I ran into problems with page margins. 我在页边距方面遇到了问题。 If I need absolute control over the whole printing area, I set the printer margins all to 0 and define the page layout exactly to the printer format. 如果我需要对整个打印区域进行绝对控制,则将打印机页边距全部设置为0,然后将页面布局完全定义为打印机格式。
  • I would try do define a page layout/paper size that suites the POS 55m printer. 我会尝试定义适合POS 55m打印机的页面布局/纸张尺寸。 Maybe Paper.A6 gives you trouble because it is "oversized". 也许Paper.A6给您带来麻烦,因为它“太大”了。 I experienced this not when working with JavaFX and printing, but with Windows GDI printing in Delphi. 在使用JavaFX和打印时,而不是在Delphi中使用Windows GDI打印时,我没有遇到这种情况。 Since the driver layer is the same, it might be a similar problem. 由于驱动程序层相同,因此可能是类似的问题。
  • It was a good starting point to define layout node size keeping printer points, ie, 72ppi, in mind. 这是定义布局节点大小并牢记打印机点(即72ppi)的良好起点。 Simply think pixel size as printer point size. 只需将像素大小视为打印机点大小。

In fact, I had good experiences with a slightly more simple code like this (sorry, it is DIN A5 paper size example, but worked): 实际上,我在使用像这样的简单代码方面有很好的经验(对不起,这是DIN A5纸张尺寸示例,但是可以用):

PrinterJob printerJob = PrinterJob.createPrinterJob();
if (printerJob != null) {
  PageLayout pageLayout = printerJob.getPrinter().createPageLayout(Paper.A5, PageOrientation.LANDSCAPE, 0, 0, 0, 0);

  boolean success = printerJob.printPage(pageLayout, root);
  if (success) {
    printerJob.endJob();
  }
}

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM