2012-06-08 75 views
2

我试图使用标签打印机(EPSON TM-T88V是特定的)来吐出PNG图像。Java使用标签打印机打印到特定页面尺寸

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

回答

0

添加MediaPrintableArea

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

感谢您的贡献乔普,但现在我得到一个'java.lang.IllegalArgumentException异常:在javax.print.attribute.standard.MediaPrintableArea中0或负值的说法 \t。 (MediaPrintableArea.java:143)' 看来我不能初始化一个MediaPrintableArea与0的 – alwinc

+0

而如果0.05? –

+0

不...只是在页面的左上角打印小字。我不认为这是MediaPrintableArea的用途。我相信课堂只是说这个媒体的哪个领域是可以印刷的。 IE,如果只有上半部分用于打印等等。所以全屏的0.05会在左上角留下一个小点。乔普......我想你可能离答案很远 – alwinc

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

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

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