Java/2D Graphics GUI/Print Job — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 06:53, 1 июня 2010
Содержание
- 1 Cancelling a Print Job
- 2 Determining Print Job Capabilities Supported by a Print Service
- 3 Determining When a Print Job Has Finished
- 4 Getting the Default Value of a Print Job Capability
- 5 Getting the Possible Values for a Print Job Capability
- 6 Listening for Print Job Attribute Changes
- 7 Listening for Print Job Status Changes
- 8 Setting the Number of Copies of a Print Job
- 9 Setting the Orientation of a Print Job -- Portrait or Landscape
Cancelling a Print Job
import java.awt.print.PrinterAbortException;
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.CancelablePrintJob;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
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[] argv) throws Exception {
try {
OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
.lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
StreamPrintService service = factories[0].getPrintService(fos);
final DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
if (job instanceof CancelablePrintJob) {
CancelablePrintJob cancelJob = (CancelablePrintJob) job;
try {
cancelJob.cancel();
} catch (PrintException e) {
}
}
job.print(doc, null);
pjDone.waitForDone();
is.close();
} catch (PrintException e) {
if (e.getCause() instanceof PrinterAbortException) {
System.out.println("Print job was cancelled");
}
}
}
}
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) {
}
}
}
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 When a Print Job Has Finished
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[] argv) throws Exception {
OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
.lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
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();
}
}
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) {
}
}
}
Getting the Default Value of a Print Job Capability
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.print.DocFlavor;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.Attribute;
import javax.print.attribute.standard.Destination;
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 attr = (Attribute) service.getDefaultAttributeValue(Destination.class);
// attr == null if the attribute is not supported
if (attr != null) {
String attrName = attr.getName();
// Get string representation of default value
String attrValue = attr.toString();
}
}
}
Getting the Possible Values for a Print Job Capability
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.standard.OrientationRequested;
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 category = OrientationRequested.class;
Object o = service.getSupportedAttributeValues(category, null, null);
if (o == null) {
System.out.println("Attribute is not supported");
} else if (o.getClass() == category) {
System.out.println("irrelevant");
} else if (o.getClass().isArray()) {
System.out.println("array");
for (int i = 0; i < Array.getLength(o); i++) {
Object v = Array.get(o, i);
System.out.println(v);
}
}
}
}
Listening for Print Job Attribute 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.attribute.Attribute;
import javax.print.attribute.HashPrintJobAttributeSet;
import javax.print.attribute.PrintJobAttributeSet;
import javax.print.attribute.standard.JobMediaSheetsCompleted;
import javax.print.event.PrintJobAttributeEvent;
import javax.print.event.PrintJobAttributeListener;
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();
PrintJobAttributeSet set = new HashPrintJobAttributeSet(job.getAttributes());
set.add(new JobMediaSheetsCompleted(0));
job.addPrintJobAttributeListener(new MyPrintJobAttributeListener(), set);
}
}
class MyPrintJobAttributeListener implements PrintJobAttributeListener {
public void attributeUpdate(PrintJobAttributeEvent pjae) {
Attribute[] attrs = pjae.getAttributes().toArray();
for (int i = 0; i < attrs.length; i++) {
String attrName = attrs[i].getName();
String attrValue = attrs[i].toString();
}
}
}
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) {
}
}
Setting the Number of Copies of a Print Job
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
public class Main {
public static void main(String[] argv) throws Exception {
// Set up the attribute set
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(2));
}
}
Setting the Orientation of a Print Job -- Portrait or Landscape
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.OrientationRequested;
public class Main {
public static void main(String[] argv) throws Exception {
// Set up the attribute set
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
// aset.add(OrientationRequested.LANDSCAPE);
}
}