import java.awt.PrintJob;
import java.io.*;
import javax.print.*;
public class Test11 {
public static void main(String[] args) {
try {
// Open the image file
InputStream is = new BufferedInputStream(
new FileInputStream("c:\\gif-file.gif"));
// Prepare the output file to receive the postscript
OutputStream fos = new BufferedOutputStream(
new FileOutputStream("c:\\ps-file.ps"));
// Find a factory that can do the conversion
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);
// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
// Monitor print job events; for the implementation of PrintJobWatcher,
// see e702 Determining When a Print Job Has Finished
PrintJobWatcher pjDone = new PrintJobWatcher(job);
// Print it
job.print(doc, null);
// Wait for the print job to be done
pjDone.waitForDone();
// It is now safe to close the streams
}
is.close();
fos.close();
} catch (PrintException e) {
} catch (IOException e) {
}
}
}
/// und jetzt die andere klasse
import javax.print.DocPrintJob;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
class PrintJobWatcher {
// true iff it is safe to close the print job's input stream
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}