3

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

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.

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);
        }
    }
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
alwinc
  • 1,317
  • 3
  • 14
  • 21

3 Answers3

4

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.

0

Add a MediaPrintableArea:

das.add(new MediaPrintableArea(0.05, 0.05, 0.05, 0.05, MediaPrintableArea.INCH));
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thanks for the contribution Joop, but I now get an `java.lang.IllegalArgumentException: 0 or negative value argument at javax.print.attribute.standard.MediaPrintableArea.(MediaPrintableArea.java:143)` It seems I cannot initialize a MediaPrintableArea with 0's – alwinc Jun 10 '12 at 02:57
  • Nope... just small print on the top left corner of the page. I don't think that is what MediaPrintableArea is for. I believe that class is merely to say which area of that media is allowable for printing. IE, if only top half is used to print etc. So 0.05 across the board makes a small dot on the top left. Joop... I think you might be far off from the answer – alwinc Jun 12 '12 at 12:56
0

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));
Lai
  • 1,320
  • 2
  • 13
  • 24