Java by API/javax.print/PrintService — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:32, 31 мая 2010

PrintService: addPrintServiceAttributeListener(PrintServiceAttributeListener listener)

  
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.Attribute;
import javax.print.event.PrintServiceAttributeEvent;
import javax.print.event.PrintServiceAttributeListener;
public class Main {
  public static void main(String[] argv) throws Exception {
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    for (int i = 0; i < services.length; i++) {
      services[i].addPrintServiceAttributeListener(new MyPrintServiceAttributeListener());
    }
  }
}
class MyPrintServiceAttributeListener implements PrintServiceAttributeListener {
  public void attributeUpdate(PrintServiceAttributeEvent psae) {
    PrintService service = psae.getPrintService();
    Attribute[] attrs = psae.getAttributes().toArray();
    for (int i = 0; i < attrs.length; i++) {
      String attrName = attrs[i].getName();
      String attrValue = attrs[i].toString();
    }
  }
}





PrintService: createPrintJob()

 
import java.io.FileInputStream;
import java.io.IOException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
public class PrintImage {
  static public void main(String args[]) throws Exception {
    try {
      PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
      pras.add(new Copies(1));
      PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
      if (pss.length == 0)
        throw new RuntimeException("No printer services available.");
      PrintService ps = pss[0];
      System.out.println("Printing to " + ps);
      DocPrintJob job = ps.createPrintJob();
      FileInputStream fin = new FileInputStream("YOurImageFileName.PNG");
      Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
      job.print(doc, pras);
      fin.close();
    } catch (IOException ie) {
      ie.printStackTrace();
    } catch (PrintException pe) {
      pe.printStackTrace();
    }
  }
}





PrintService: getDefaultAttributeValue(Class<? extends Attribute> arg0)

  
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import javax.print.DocFlavor;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.Attribute;
public class Main {
  public static void main(String[] argv) throws Exception {
    OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
        .lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
    StreamPrintService service = factories[0].getPrintService(fos);
    Class[] cats = service.getSupportedAttributeCategories();
    for (int j = 0; j < cats.length; j++) {
      Attribute attr = (Attribute) service.getDefaultAttributeValue(cats[j]);
      if (attr != null) {
        String attrName = attr.getName();
        String attrValue = attr.toString();
        Object o = service.getSupportedAttributeValues(attr.getCategory(), null, null);
        if (o.getClass().isArray()) {
          for (int k = 0; k < Array.getLength(o); k++) {
            Object o2 = Array.get(o, k);
            System.out.println(o2);
          }
        }
      }
    }
  }
}





PrintService: getSupportedAttributeCategories()

  
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import javax.print.DocFlavor;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.Attribute;
public class Main {
  public static void main(String[] argv) throws Exception {
    OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
        .lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
    StreamPrintService service = factories[0].getPrintService(fos);
    Class[] cats = service.getSupportedAttributeCategories();
    for (int j = 0; j < cats.length; j++) {
      Attribute attr = (Attribute) service.getDefaultAttributeValue(cats[j]);
      if (attr != null) {
        String attrName = attr.getName();
        String attrValue = attr.toString();
        Object o = service.getSupportedAttributeValues(attr.getCategory(), null, null);
        if (o.getClass().isArray()) {
          for (int k = 0; k < Array.getLength(o); k++) {
            Object o2 = Array.get(o, k);
            System.out.println(o2);
          }
        }
      }
    }
  }
}





PrintService: getSupportedDocFlavors()

  
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.Attribute;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.PrinterName;
public class Main {
  static public void main(String args[]) throws Exception {
    PrintService pss[] = PrintServiceLookup.lookupPrintServices(null, null);
    for (int i = 0; i < pss.length; ++i) {
      System.out.println(pss[i]);
      PrintService ps = pss[i];
      PrintServiceAttributeSet psas = ps.getAttributes();
      Attribute attributes[] = psas.toArray();
      for (int j = 0; j < attributes.length; ++j) {
        Attribute attribute = attributes[j];
        System.out.println("  attribute: " + attribute.getName());
        if (attribute instanceof PrinterName) {
          System.out.println("    printer name: " + ((PrinterName) attribute).getValue());
        }
      }
      DocFlavor supportedFlavors[] = ps.getSupportedDocFlavors();
      for (int j = 0; j < supportedFlavors.length; ++j) {
        System.out.println("  flavor: " + supportedFlavors[j]);
      }
    }
  }
}