Java/2D Graphics GUI/Print Service
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
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());
}
}
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) {
}
}
}