Java -Check if file is in print Queue / In Use

后端 未结 1 726
长发绾君心
长发绾君心 2020-12-11 07:11

OK I have a program that:

  1. Creates a temporary file based on a users input
  2. Prints the File(Optional)
  3. Deletes the File (Optional)
<
相关标签:
1条回答
  • 2020-12-11 07:57

    Have you checked the Java Print API? From http://download.oracle.com/javase/1.4.2/docs/api/javax/print/event/PrintJobListener.html:

    public interface

    PrintJobListener

    Implementations of this listener interface should be attached to a DocPrintJob to monitor the status of the printer job.

    I imagine you can submit a print job and then monitor its status through this.

    There's also a fairly complete example at exampledepot.com/egs/javax.print/WaitForDone.html: (Note: URL seems to have changed, and points to potential malware)

    try {
        // Open the image file
        InputStream is = new BufferedInputStream(
            new FileInputStream("filename.gif"));
        // Create the print job
        DocPrintJob job = service.createPrintJob();
        Doc doc = new SimpleDoc(is, flavor, null);
    
        // Monitor print job events
        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) {
    }
    
    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) {
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题