Java使用标签打印机打印到特定页面大小

时间:2012-06-08 15:05:08

标签: java printing thermal-printer

我正在尝试使用标签打印机(具体为EPSON TM-T88V)来吐出PNG图像。

我可以打印得很好,除非我正在打印图像尺寸(220x175在72dpi再次具体),打印图像顶部有一沓白色空间,我认为这是浪费纸张

关于如何最大限度地减少纸张浪费的任何想法?我希望它只打印图像,最小的空白然后剪切纸张。

这是我的代码

    AttributeSet aset = new HashAttributeSet();
    aset.add(new PrinterName(printerName, null));
    /* locate a print service that can handle the request */
    PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PNG, aset);

    if (services.length >= 1) {
        /* create a print job for the chosen service */
        DocPrintJob pj = services[0].createPrintJob();

        DocAttributeSet das = new HashDocAttributeSet();
        das.add(PrintQuality.HIGH);
        das.add(MediaSizeName.ISO_A7); // I know the problem is here somewhere. This Media size seems to work best currently

        try {
            /* 
            * Create a Doc object to hold the print data.
            */
            Doc doc = new SimpleDoc(imageByteIs, DocFlavor.INPUT_STREAM.PNG, das);

            /* print the doc as specified */
            pj.print(doc, null);

        } catch (PrintException e) { 
            System.err.println(e);
        }
    }

3 个答案:

答案 0 :(得分:3)

您可以通过以下方式找到可用的纸张尺寸:

PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Media[] res = (Media[]) printService.getSupportedAttributeValues(Media.class, null, null);
for (Media media : res) {
    if (media instanceof MediaSizeName) {
        MediaSizeName msn = (MediaSizeName) media;
        MediaSize ms = MediaSize.getMediaSizeForName(msn);
        float width = ms.getX(MediaSize.INCH);
        float height = ms.getY(MediaSize.INCH);
        System.out.println(media + ": width = " + width + "; height = " + height);
    }
}

找到最适合您纸张尺寸的可用MediaSizeName后,只需将其添加到MediaSizeName.ISO_A7。

答案 1 :(得分:0)

添加MediaPrintableArea

das.add(new MediaPrintableArea(0.05, 0.05, 0.05, 0.05, MediaPrintableArea.INCH));

答案 2 :(得分:0)

这是我用于在A4纸上打印10mm边距的设置。

int width = Math.round(MediaSize.ISO.A4.getX(MediaSize.MM));
int height = Math.round(MediaSize.ISO.A4.getY(MediaSize.MM));
das.add(new MediaPrintableArea(10, 10, width-20, height-20, MediaPrintableArea.MM));