|
mes123 (Programmer) |
23 Sep 05 7:34 |
Thanks sedj, This is a useful start, but like so many of the examples it deals with printing images and postscript files. I just want to print ascii text. How would I substitute the input stream for a text string? here is my test code - but I cannot get the DocFlavor right so cannot test it yet. CODEimport java.io.*; import javax.print.*; import javax.print.attribute.*; import javax.print.attribute.standard.*; import javax.print.event.*;
public class printtest { public static void main(String[] args) { try { // Open the image file InputStream is = new BufferedInputStream( new FileInputStream("filename.gif")); String testdata = "hello world \n have a nice day."; // Find the default service // DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF; // DocFlavor flavor = DocFlavor."text/plain"; <<-this will not compile // DocFlavor flavor = DocFlavor.STRING; // <<-this will not compile PrintService service = PrintServiceLookup.lookupDefaultPrintService(); // Create the print job DocPrintJob job = service.createPrintJob(); Doc doc= new SimpleDoc(testdata, 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 input stream is.close(); } catch (PrintException e) { } catch (IOException e) { } } static 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) { } } } } |
|