Java Tutorial/2D Graphics/Print Service
Содержание
- 1 Determining Print Job Capabilities Supported by a Print Service
- 2 Determining the Capabilities of a Print Service
- 3 Discovering Available Print Services
- 4 Discovering Available Streaming Print Services
- 5 Listening for Print Job Status Changes
- 6 Listening for Print Service Status Changes
- 7 List Print Services
- 8 Use a factory to create a print service
- 9 Using a Printing Service
- 10 Using a Streaming Printing Service
Determining Print Job Capabilities Supported by a Print Service
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);
}
}
}
}
}
}
Determining the Capabilities of a Print Service
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
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);
Attribute[] attrs = service.getAttributes().toArray();
for (int j = 0; j < attrs.length; j++) {
String attrName = attrs[j].getName();
String attrValue = attrs[j].toString();
System.out.println(attrName);
System.out.println(attrValue);
}
}
}
Discovering Available Print Services
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.ColorSupported;
import javax.print.attribute.standard.PrinterName;
public class Main {
public static void main(String[] argv) throws Exception {
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
services = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, null);
AttributeSet aset = new HashAttributeSet();
aset.add(new PrinterName("HP LaserJet", null));
services = PrintServiceLookup.lookupPrintServices(null, aset);
aset = new HashAttributeSet();
aset.add(ColorSupported.SUPPORTED);
services = PrintServiceLookup.lookupPrintServices(null, aset);
}
}
Discovering Available Streaming Print Services
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.print.DocFlavor;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
public class Main {
public static void main(String[] argv) throws Exception {
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
.lookupStreamPrintServiceFactories(null, null);
factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
DocFlavor.INPUT_STREAM.GIF, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
}
}
Listening for Print Job Status Changes
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
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);
DocPrintJob job = service.createPrintJob();
job.addPrintJobListener(new MyPrintJobListener());
}
}
class MyPrintJobListener implements PrintJobListener {
public void printDataTransferCompleted(PrintJobEvent pje) {
System.out.println("printDataTransferCompleted");
}
public void printJobCanceled(PrintJobEvent pje) {
System.out.println("The print job was cancelled");
}
public void printJobCompleted(PrintJobEvent pje) {
System.out.println("The print job was completed");
}
public void printJobFailed(PrintJobEvent pje) {
System.out.println("The print job has failed");
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
}
public void printJobRequiresAttention(PrintJobEvent pje) {
}
}
Listening for Print Service Status Changes
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();
}
}
}
List Print Services
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]);
}
}
}
}
Use a factory to create a print service
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.print.DocFlavor;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
public class Main {
public static void main(String[] argv) throws Exception {
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
.lookupStreamPrintServiceFactories(null, null);
OutputStream fos = new BufferedOutputStream(new FileOutputStream("outfile.ps"));
StreamPrintService service = factories[0].getPrintService(fos);
}
}
Using a Printing Service
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
public class Main {
public static void main(String[] args) throws Exception {
InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
is.close();
}
}
class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobCompleted(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobFailed(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
Using a Streaming Printing Service
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.SimpleDoc;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
public class Main {
public static void main(String[] args) throws Exception{
InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));
OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
.lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
if (factories.length > 0) {
StreamPrintService service = factories[0].getPrintService(fos);
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
}
is.close();
fos.close();
}
}
class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobCompleted(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobFailed(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}