Java Printing to specific page size using label printer

馋奶兔 提交于 2019-12-03 09:04:40

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.

Add a MediaPrintableArea:

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

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

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!