Java Printer Usage getPrinterAttributes(PrintService printer)

Here you can find the source of getPrinterAttributes(PrintService printer)

Description

get Printer Attributes

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
public static Set<Attribute> getPrinterAttributes(PrintService printer) 

Method Source Code

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

import java.util.Arrays;

import java.util.LinkedHashSet;

import java.util.Set;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;

public class Main {

    @SuppressWarnings("unchecked")
    public static Set<Attribute> getPrinterAttributes(PrintService printer) {
        Set<Attribute> set = new LinkedHashSet<>();

        //get the supported docflavors, categories and attributes
        Class<? extends Attribute>[] categories = (Class<? extends Attribute>[]) printer
                .getSupportedAttributeCategories();
        DocFlavor[] flavors = printer.getSupportedDocFlavors();
        AttributeSet attributes = printer.getAttributes();

        //get all the avaliable attributes
        for (Class<? extends Attribute> category : categories) {
            for (DocFlavor flavor : flavors) {
                //get the value
                Object value = printer.getSupportedAttributeValues(category, flavor, attributes);

                //check if it's something
                if (value != null) {
                    //if it's a SINGLE attribute...
                    if (value instanceof Attribute) {
                        set.add((Attribute) value); //...then add it
                    } //if it's a SET of attributes...
                    else if (value instanceof Attribute[]) {
                        set.addAll(Arrays.asList((Attribute[]) value)); //...then add its childs
                    }/*from  w  w  w. j  av  a2s.  c  o m*/
                }
            }
        }
        return set;
    }

    public static Set<Attribute> getPrinterAttributes(String printerName) {
        PrintService ps = findPrintService(printerName);
        return getPrinterAttributes(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. getEnumAttribute(String name, Object value)
  2. getIntegerAttribute(String name, int value)
  3. getJobPriority(int pages, int copies, boolean withDialog)
  4. getOrientationRequested(int value)
  5. getPaperTraysArray(PrintService ps)
  6. getPrinterJobProperties(PrintService ps)
  7. getPrinterNames()
  8. getPrintService(String printername)
  9. getPrintServices()