如何在JavaFX中打印到特定的纸张布局

时间:2016-12-16 13:07:40

标签: java javafx printing javafx-8

我看了thisthis,但我无法打印到Brother QL-700标签打印机。我总是得到以下例外:

java.lang.IllegalArgumentException: Bad margins

我要打印的标签是38mm X 30mm。这是我的代码:

Printer printer = Printer.getDefaultPrinter();
Paper label = PrintHelper.createPaper("38x30", 38, 30, Units.MM);
PageLayout pageLayout = printer.createPageLayout(label, PageOrientation.LANDSCAPE, Printer.MarginType.EQUAL);

PrinterJob job = PrinterJob.createPrinterJob();
double scaleX = pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();
double scaleY = pageLayout.getPrintableHeight() / node.getBoundsInParent().getHeight();
node.getTransforms().add(new Scale(scaleX, scaleY));
if (job != null  && job.showPrintDialog(node.getScene().getWindow()) ) {
boolean success = job.printPage(node);
    if (success) {
        job.endJob();
    }
}

更新1:

我在这里得到例外:

job.showPrintDialog(node.getScene().getWindow())

更新2:

printer.getPrinterAttributes().getSupportedPapers()

告诉我:

[Paper: 12mm size=12.0x89.8 MM, Paper: 12mm Dia size=12.0x12.0 MM, Paper: 12mm x 2 size=21.0x89.8 MM, Paper: 12mm x 3 size=30.0x89.8 MM, Paper: 12mm x 4 size=39.0x89.8 MM, Paper: 17mm x 54mm size=17.0x53.9 MM, Paper: 17mm x 87mm size=17.0x86.9 MM, Paper: 23mm x 23mm size=23.0x23.0 MM, Paper: 24mm Dia size=24.0x24.0 MM, Paper: 29mm size=29.0x89.8 MM, Paper: 29mm x 2 size=54.8x89.8 MM, Paper: 29mm x 3 size=80.7x89.8 MM, Paper: 29mm x 42mm size=29.0x41.9 MM, Paper: 29mm x 90mm size=29.0x89.8 MM, Paper: 38mm size=38.0x89.8 MM, Paper: 38mm x 2 size=73.0x89.8 MM, Paper: 38mm x 90mm size=38.0x89.8 MM, Paper: 39mm x 48mm size=39.0x47.8 MM, Paper: 50mm size=50.0x89.8 MM, Paper: 54mm size=53.8x89.8 MM, Paper: 58mm Dia size=58.3x58.3 MM, Paper: 60mm x 86mm size=60.0x86.8 MM, Paper: 62mm size=62.0x89.8 MM, Paper: 62mm x 100mm size=62.0x99.8 MM, Paper: Absender-Etikett size=17.0x53.9 MM, Paper: Adress-Etikett (Standard) size=29.0x89.8 MM, Paper: Adress-Etikett (groß) size=38.0x89.8 MM, Paper: Benutzerdefinierte Größe size=29.0x89.8 MM, Paper: CD/DVD-Etikett size=58.3x58.3 MM, Paper: Hängeregister-Etikett size=17.0x53.9 MM, Paper: Mappe 3 cm - Rücken size=29.0x209.1 MM, Paper: Mappe 5 cm - Rücken size=62.0x209.1 MM, Paper: Namensschild size=62.0x99.8 MM, Paper: Ordnerregister-Etikett size=17.0x86.9 MM, Paper: Porto-Etikett 62mmx184mm size=62.0x183.9 MM, Paper: Porto-Etikett 62mmx209mm size=62.0x209.3 MM, Paper: Quadratisches Etikett size=23.0x23.0 MM, Paper: Rundes Etikett ø 12 mm size=12.0x12.0 MM, Paper: Rundes Etikett ø 24 mm size=24.0x24.0 MM, Paper: Versand-Etikett (Päckchen) size=62.0x99.8 MM]

更新3

我发现public PageLayout createPageLayout(Paper paper, PageOrientation orient, MarginType mType)类中的函数Printer将边距乘以72(我认为是dpi)。由于这一点,我认为我的利润率很低。但我不明白为什么。我也将所有边距设置为零,结果相同。 PageLayout pageLayout = printer.createPageLayout(tmp, PageOrientation.PORTRAIT,0f,0f,0f,0f);

我尝试this并且它有效。但仅限于给定的大小double width = fromCMToPPI(3.5); double height = fromCMToPPI(8.8);。如果我将高度设置为3.3则无法正常工作

更新4

我可以用这段代码打印:

DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Media[] res = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);

aset.add(OrientationRequested.PORTRAIT);
aset.add(res[14]);

DocPrintJob pj = printService.createPrintJob();

FileInputStream fis = new FileInputStream("test.jpg");
Doc doc = new SimpleDoc(fis, flavor, null);
pj.print(doc, aset);

但现在问题是身高。我有很多空间。如何设置纸张的高度?顺便说一下它的无穷无尽的纸张和res [14]意味着38毫米无穷无尽。

0 个答案:

没有答案