gpt4 book ai didi

java - 如何知道打印机的打印方法何时完成打印文件?

转载 作者:行者123 更新时间:2023-12-01 15:36:09 27 4
gpt4 key购买 nike

我正在开发一个打印文件的 JAVA 程序。我需要知道打印机何时完成打印文件,我发现这个简单的代码很有趣:

import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.PrinterStateReason;
import javax.print.attribute.standard.PrinterStateReasons;
import javax.print.attribute.standard.Severity;
import javax.print.event.*;
import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.*;
import java.util.Set;

/**
* PrintTest
*/
public class PrintTest implements PrintServiceAttributeListener,PrintJobListener,Doc, Printable, PrintJobAttributeListener {

private static final transient String TEXT = "12345";

public static void main(String[] args) {
PrintTest test = new PrintTest();
test.checkPrinters();
}

public void checkPrinters() {
Thread newThread = new Thread(new Runnable() {
public void run() {
PrintService ps = PrinterJob.getPrinterJob().getPrintService();

DocFlavor[] myFlavors = ps.getSupportedDocFlavors();
ps.addPrintServiceAttributeListener(PrintTest.this);
DocPrintJob docJob = ps.createPrintJob();
docJob.addPrintJobAttributeListener(PrintTest.this, null);
docJob.addPrintJobListener(PrintTest.this);
try {
docJob.print(PrintTest.this,null);
}
catch (PrintException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} });

newThread.start();
/**
PrintServiceAttributeSet attSet = ps.getAttributes();
PrinterStateReasons psr = ps.getAttribute(PrinterStateReasons.class);

if (psr != null) {
Set<PrinterStateReason> errors = psr.printerStateReasonSet(Severity.REPORT);
for (PrinterStateReason reason : errors)
System.out.printf(" Reason : %s",reason.getName());
System.out.println();
} */
}

public void attributeUpdate(PrintServiceAttributeEvent psae) {
System.out.println(psae.getAttributes());
}

public void printDataTransferCompleted(PrintJobEvent pje) {
System.out.println("Transfer completed");
}

public void printJobCompleted(PrintJobEvent pje) {
System.out.println("Completed");
}

public void printJobFailed(PrintJobEvent pje) {
System.out.println("Failed");
PrinterStateReasons psr = pje.getPrintJob().getPrintService().getAttribute(PrinterStateReasons.class);
if (psr != null) {
Set<PrinterStateReason> errors = psr.printerStateReasonSet(Severity.REPORT);
for (PrinterStateReason reason : errors)
System.out.printf(" Reason : %s",reason.getName());
System.out.println();
}
}

public void printJobCanceled(PrintJobEvent pje) {
System.out.println("Canceled");
}

public void printJobNoMoreEvents(PrintJobEvent pje) {
System.out.println("No more events");
}

public void printJobRequiresAttention(PrintJobEvent pje) {
System.out.println("Job requires attention");
PrinterStateReasons psr = pje.getPrintJob().getPrintService().getAttribute(PrinterStateReasons.class);
if (psr != null) {
Set<PrinterStateReason> errors = psr.printerStateReasonSet(Severity.REPORT);
for (PrinterStateReason reason : errors)
System.out.printf(" Reason : %s",reason.getName());
System.out.println();
}
}

public DocFlavor getDocFlavor() {
return DocFlavor.SERVICE_FORMATTED.PRINTABLE; //To change body of implemented methods use File | Settings | File Templates.
}

public Object getPrintData() throws IOException {
return this;
}

public DocAttributeSet getAttributes() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public Reader getReaderForText() throws IOException {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public InputStream getStreamForBytes() throws IOException {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
return pageIndex == 0 ? PAGE_EXISTS : NO_SUCH_PAGE; //To change body of implemented methods use File | Settings | File Templates.
}

public void attributeUpdate(PrintJobAttributeEvent pjae) {
System.out.println("Look out");
}
}

所以,我感兴趣的方法是:

public void printJobCompleted(PrintJobEvent pje) {
System.out.println("Completed");
}

但这对我不起作用。问题是打印机不支持这种类型的代码。谁能告诉我支持此代码的打印机型号?

最佳答案

看示例代码here :

看起来您需要实现您的 PrintJobAdapter。相关位是:

package com.your.package;

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 DetermineThatPrintJobHasFinished {

public static void main(String[] args) throws Exception {
//... see example in link

// Locate the default print service for this environment.
PrintService service = PrintServiceLookup.lookupDefaultPrintService();

// Create and return a PrintJob capable of handling data from
// any of the supported document flavors.
DocPrintJob printJob = service.createPrintJob();

// register a listener to get notified when the job is complete
JobCompleteMonitor monitor = new JobCompleteMonitor();
printJob.addPrintJobListener(monitor);

// Construct a SimpleDoc with the specified print data, doc flavor and doc attribute set.
Doc doc = new SimpleDoc(is, flavor, null);

// Print a document with the specified job attributes.
printJob.print(doc, null);
monitor.waitForJobCompletion();

is.close();
}

private static class JobCompleteMonitor extends PrintJobAdapter {
private boolean completed = false;

@Override
public void printJobCanceled(PrintJobEvent pje) {
signalCompletion();
}

@Override
public void printJobCompleted(PrintJobEvent pje) {
signalCompletion();
}

@Override
public void printJobFailed(PrintJobEvent pje) {
signalCompletion();
}

@Override
public void printJobNoMoreEvents(PrintJobEvent pje) {
signalCompletion();
}

private void signalCompletion() {
synchronized (JobCompleteMonitor.this) {
completed = true;
JobCompleteMonitor.this.notify();
}
}

public synchronized void waitForJobCompletion() {
try {
while (!completed) {
wait();
}
} catch (InterruptedException e) {
}
}
}
}

关于java - 如何知道打印机的打印方法何时完成打印文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28054191/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com