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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10951394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:12:28  来源:igfitidea点击:

Java Printing to specific page size using label printer

javaprintingthermal-printer

提问by alwinc

I am trying to use a label printer (EPSON TM-T88V to be specific), to spit out PNG images.

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

I can get it to print fine, except when I am printing an image dimensions (220x175 at 72dpi to be specific again) there is a wad of white space on top of the image printed, which I think is a waste of paper.

我可以让它打印得很好,除非我打印图像尺寸(220x175 72dpi 再次具体)打印的图像顶部有一团空白,我认为这是浪费纸张。

Any ideas on how I can minimize the paper waste? I want it to print just the image, minimal whitespace and then cut the paper.

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

Here is my code

这是我的代码

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

回答by Some Java Programmer

You can find the available paper sizes via:

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

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

Once you've found the available MediaSizeName that most closely fits your paper size, simply add it in place of MediaSizeName.ISO_A7.

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

回答by Joop Eggen

Add a MediaPrintableArea:

添加一个MediaPrintableArea

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

回答by Lai

This is the settings I use for printing on A4 paper with 10mm margin.

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

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