importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjavax.print.Doc;
importjavax.print.DocFlavor;
importjavax.print.DocPrintJob;
importjavax.print.SimpleDoc;
importjavax.print.StreamPrintService;
importjavax.print.StreamPrintServiceFactory;
importjavax.print.event.PrintJobAdapter;
importjavax.print.event.PrintJobEvent;
publicclassMain{
publicstaticvoidmain(String[]args)throwsException{
InputStreamis=newBufferedInputStream(newFileInputStream("filename.gif"));
OutputStreamfos=newBufferedOutputStream(newFileOutputStream("filename.ps"));
DocFlavorflavor=DocFlavor.INPUT_STREAM.GIF;
StreamPrintServiceFactory[]factories=StreamPrintServiceFactory
.lookupStreamPrintServiceFactories(flavor,DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
if(factories.length>0){
StreamPrintServiceservice=factories[0].getPrintService(fos);
DocPrintJobjob=service.createPrintJob();
Docdoc=newSimpleDoc(is,flavor,null);
PrintJobWatcherpjDone=newPrintJobWatcher(job);
job.print(doc,null);
pjDone.waitForDone();
}
is.close();
fos.close();
}
}
classPrintJobWatcher{
booleandone=false;
PrintJobWatcher(DocPrintJobjob){
job.addPrintJobListener(newPrintJobAdapter(){
publicvoidprintJobCanceled(PrintJobEventpje){
synchronized(PrintJobWatcher.this){
done=true;
PrintJobWatcher.this.notify();
}
}
publicvoidprintJobCompleted(PrintJobEventpje){
synchronized(PrintJobWatcher.this){
done=true;
PrintJobWatcher.this.notify();
}
}
publicvoidprintJobFailed(PrintJobEventpje){
synchronized(PrintJobWatcher.this){
done=true;
PrintJobWatcher.this.notify();
}
}
publicvoidprintJobNoMoreEvents(PrintJobEventpje){
synchronized(PrintJobWatcher.this){
done=true;
PrintJobWatcher.this.notify();
}
}
});
}
publicsynchronizedvoidwaitForDone(){
try{
while(!done){
wait();
}
}catch(InterruptedExceptione){
}
}
}
在Java中使用流印刷服务
最新推荐文章于 2022-10-24 22:00:00 发布