【问题标题】:Java Printing to specific page size using label printer使用标签打印机将 Java 打印到特定页面大小
【发布时间】:2012-06-12 15:55:17
【问题描述】:

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

我可以让它打印得很好,除非我打印图像尺寸(220x175 at 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);
        }
    }

【问题讨论】:

    标签: java printing thermal-printer


    【解决方案1】:

    添加MediaPrintableArea

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

    【讨论】:

    • 感谢 Joop 的贡献,但我现在得到一个 java.lang.IllegalArgumentException: 0 or negative value argument at javax.print.attribute.standard.MediaPrintableArea.<init>(MediaPrintableArea.java:143) 似乎我无法用 0 初始化 MediaPrintableArea
    • 不...只是页面左上角的小字体。我认为这不是 MediaPrintableArea 的用途。我相信课程只是说该媒体的哪个区域可以打印。 IE,如果只有上半部分用于打印等。所以 0.05 全线在左上角形成一个小点。乔普……我想你可能离答案还很远
    【解决方案2】:

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

    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 的位置即可。

    【讨论】:

      【解决方案3】:

      这是我在 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));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-10
        • 1970-01-01
        • 2016-06-02
        • 1970-01-01
        • 1970-01-01
        • 2010-12-11
        相关资源
        最近更新 更多