Java使用标签打印机打印到特定页面大小 [英] Java Printing to specific page size using label printer
问题描述
我正在尝试使用标签打印机(具体为EPSON TM-T88V)来吐出PNG图像。
I am trying to use a label printer (EPSON TM-T88V to be specific), to spit out PNG images.
我可以将它打印得很好,除了当我打印图像尺寸(220x175在72dpi再次具体)时,打印图像顶部有一沓白色空间,我认为这是浪费纸张。
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.
关于如何最大限度地减少纸张浪费的任何想法?我希望它只打印图像,最小的空白然后剪切纸张。
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.
这是我的代码
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);
}
}
推荐答案
你可以通过以下方式找到可用的纸张尺寸:
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);
}
}
找到最接近的可用MediaSizeName后适合您的纸张尺寸,只需将其添加到MediaSizeName.ISO_A7。
Once you've found the available MediaSizeName that most closely fits your paper size, simply add it in place of MediaSizeName.ISO_A7.
这篇关于Java使用标签打印机打印到特定页面大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!