Java Printer Usage getPaperTraysArray(PrintService ps)

Here you can find the source of getPaperTraysArray(PrintService ps)

Description

get Paper Trays Array

License

Open Source License

Declaration

public static Media[] getPaperTraysArray(PrintService ps) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

import javax.print.attribute.standard.Media;

import javax.print.attribute.standard.MediaTray;

public class Main {

    public static Media[] getPaperTraysArray(PrintService ps) {
        List<Media> list = getPaperTrays(ps);
        return Arrays.copyOf(list.toArray(), list.size(), Media[].class);
    }//from  w  ww . jav  a2s. c o m

    public static Media[] getPaperTraysArray(String printerName) {
        PrintService ps = findPrintService(printerName);
        return getPaperTraysArray(ps);
    }

    public static List<Media> getPaperTrays(PrintService ps) {
        Media[] medias = (Media[]) ps.getSupportedAttributeValues(Media.class, null, null);
        List<Media> list = new ArrayList<>();
        for (Media media : medias) {
            if (media instanceof MediaTray) {
                list.add(media);
            }
        }
        return list;
    }

    public static List<Media> getPaperTrays(String printerName) {
        PrintService ps = findPrintService(printerName);
        return getPaperTrays(ps);
    }

    public static PrintService findPrintService(String printerName) {
        PrintService printService = null;
        if (printerName == null || printerName.isEmpty()) {
            printService = PrintServiceLookup.lookupDefaultPrintService();
        } else {
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
            for (PrintService service : services) {
                String prtName = service.getName();
                if (prtName.contains(printerName)) {
                    printService = service;
                    break;
                }
            }
        }
        return printService;
    }
}

Related

  1. getDefaultPrintRequestAttributes()
  2. getEnumAttribute(String name, Object value)
  3. getIntegerAttribute(String name, int value)
  4. getJobPriority(int pages, int copies, boolean withDialog)
  5. getOrientationRequested(int value)
  6. getPrinterAttributes(PrintService printer)
  7. getPrinterJobProperties(PrintService ps)
  8. getPrinterNames()
  9. getPrintService(String printername)