下面列出了java.awt.print.PrinterIOException#javax.print.PrintService 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected static PrintService lookupDefaultPrintService() {
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
/* Pageable implies Printable so checking both isn't strictly needed */
if (service != null &&
service.isDocFlavorSupported(
DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&
service.isDocFlavorSupported(
DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
return service;
} else {
PrintService []services =
PrintServiceLookup.lookupPrintServices(
DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
if (services.length > 0) {
return services[0];
}
}
return null;
}
public static void main(String[] args) throws Exception {
job = PrinterJob.getPrinterJob();
PrintService prtSrv = job.getPrintService();
if (job.getPrintService() == null) {
System.out.println("No printers. Test cannot continue");
return;
}
if (!prtSrv.isAttributeCategorySupported(JobSheets.class)) {
return;
}
SwingUtilities.invokeAndWait(() -> {
doTest(BannerTest::printTest);
});
mainThread = Thread.currentThread();
try {
Thread.sleep(180000);
} catch (InterruptedException e) {
if (!testPassed && testGeneratedInterrupt) {
throw new RuntimeException("Banner page did not print");
}
}
if (!testGeneratedInterrupt) {
throw new RuntimeException("user has not executed the test");
}
}
public static void main(String[] args) throws Exception {
for (PrintService service : PrintServiceLookup.lookupPrintServices(null, null)) {
String serviceName = service.getName();
PrintService serviceByName = lookupByName(serviceName);
if (!service.equals(serviceByName)) {
throw new RuntimeException("NOK " + serviceName
+ " expected: " + service.getClass().getName()
+ " got: " + serviceByName.getClass().getName());
}
}
System.out.println("Test PASSED");
}
synchronized PrinterGraphicsConfig getPrinterGraphicsConfig() {
if (pgConfig != null) {
return pgConfig;
}
String deviceID = "Printer Device";
PrintService service = getPrintService();
if (service != null) {
deviceID = service.toString();
}
pgConfig = new PrinterGraphicsConfig(deviceID,
defaultDeviceTransform,
deviceWidth, deviceHeight);
return pgConfig;
}
public PrintRequestAttributeSet
showDocumentProperties(PrinterJob job,
Window owner,
PrintService service,
PrintRequestAttributeSet aset) {
if (!(job instanceof WPrinterJob)) {
return null;
}
WPrinterJob wJob = (WPrinterJob)job;
return wJob.showDocumentProperties(owner, service, aset);
}
/**
* Associate this PrinterJob with a new PrintService.
*
* Throws <code>PrinterException</code> if the specified service
* cannot support the <code>Pageable</code> and
* <code>Printable</code> interfaces necessary to support 2D printing.
* @param a print service which supports 2D printing.
*
* @throws PrinterException if the specified service does not support
* 2D printing or no longer available.
*/
public void setPrintService(PrintService service)
throws PrinterException {
if (service == null) {
throw new PrinterException("Service cannot be null");
} else if (!(service instanceof StreamPrintService) &&
service.getName() == null) {
throw new PrinterException("Null PrintService name.");
} else {
// Check the list of services. This service may have been
// deleted already
PrinterState prnState = (PrinterState)service.getAttribute(
PrinterState.class);
if (prnState == PrinterState.STOPPED) {
PrinterStateReasons prnStateReasons =
(PrinterStateReasons)service.getAttribute(
PrinterStateReasons.class);
if ((prnStateReasons != null) &&
(prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN)))
{
throw new PrinterException("PrintService is no longer available.");
}
}
if (service.isDocFlavorSupported(
DocFlavor.SERVICE_FORMATTED.PAGEABLE) &&
service.isDocFlavorSupported(
DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
myService = service;
} else {
throw new PrinterException("Not a 2D print service: " + service);
}
}
}
/**
* Invoked by the RasterPrintJob super class
* this method is called after that last page
* has been imaged.
*/
protected void endDoc() throws PrinterException {
if (mPSStream != null) {
mPSStream.println(EOF_COMMENT);
mPSStream.flush();
if (mDestType != RasterPrinterJob.STREAM) {
mPSStream.close();
}
}
if (mDestType == RasterPrinterJob.PRINTER) {
PrintService pServ = getPrintService();
if (pServ != null) {
mDestination = pServ.getName();
if (isMac) {
PrintServiceAttributeSet psaSet = pServ.getAttributes();
if (psaSet != null) {
mDestination = psaSet.get(PrinterName.class).toString() ;
}
}
}
PrinterSpooler spooler = new PrinterSpooler();
java.security.AccessController.doPrivileged(spooler);
if (spooler.pex != null) {
throw spooler.pex;
}
}
}
private PrintService getNamedPrinterNameSysV(String name) {
String command = "/usr/bin/lpstat -v " + name;
String []result = execCmd(command);
if (result == null || result[0].indexOf("unknown printer") > 0) {
return null;
} else {
return new UnixPrintService(name);
}
}
protected boolean isSupportedValue(Attribute attrval,
PrintRequestAttributeSet attrset) {
PrintService ps = getPrintService();
return
(attrval != null && ps != null &&
ps.isAttributeValueSupported(attrval,
DocFlavor.SERVICE_FORMATTED.PAGEABLE,
attrset));
}
protected void updatePageAttributes(PrintService service,
PageFormat page) {
if (this.attributes == null) {
this.attributes = new HashPrintRequestAttributeSet();
}
updateAttributesWithPageFormat(service, page, this.attributes);
}
public void run() {
PrintService[] services = getPrintServices();
synchronized (this) {
BackgroundLookupListener listener;
for (int i=0; i<lookupListeners.size(); i++) {
listener =
(BackgroundLookupListener)lookupListeners.elementAt(i);
listener.notifyServices(copyOf(services));
}
lookupListeners = null;
}
}
public synchronized PrintService[] getPrintServices() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPrintJobAccess();
}
if (printServices == null) {
refreshServices();
}
return printServices;
}
@Override
public PrintService getPrintService() {
if (printService == null) {
PrintService[] lookupPrintServices = PrinterJob
.lookupPrintServices();
if (lookupPrintServices.length > 0)
printService = lookupPrintServices[0];
}
return printService;
}
public PrintRequestAttributeSet
showDocumentProperties(PrinterJob job,
Window owner,
PrintService service,
PrintRequestAttributeSet aset) {
if (!(job instanceof WPrinterJob)) {
return null;
}
WPrinterJob wJob = (WPrinterJob)job;
return wJob.showDocumentProperties(owner, service, aset);
}
private boolean matchesAttributes(PrintService service,
PrintServiceAttributeSet attributes) {
Attribute [] attrs = attributes.toArray();
Attribute serviceAttr;
for (int i=0; i<attrs.length; i++) {
serviceAttr
= service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
return false;
}
}
return true;
}
public static void doPrinterJob(String fileStr, OrientationRequested o) {
PrinterJob pj = PrinterJob.getPrinterJob();
PrintService ps = pj.getPrintService();
if (ps == null) {
System.out.println("No print service found.");
return;
}
pj.setPrintable(new PrintToDir());
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(o);
File f = new File(fileStr);
// f.deleteOnExit();
URI dest = f.toURI();
Destination d = new Destination(dest);
if (ps.isAttributeValueSupported(d, null, null)) {
aset.add(d);
try {
pj.print(aset);
} catch (PrinterException e) {
System.out.println("PrinterJob passed.");
return;
}
throw new RuntimeException("PrinterJob:PrinterException expected but not thrown. \nTEST FAILED");
} else {
System.out.println("Destination attribute is not a supported value. PrinterJob passed.");
}
}
UnixPrintJob(PrintService service) {
this.service = service;
mDestination = service.getName();
if (UnixPrintServiceLookup.isMac()) {
mDestination = ((IPPPrintService)service).getDest();
}
mDestType = UnixPrintJob.DESTPRINTER;
}
public PrintRequestAttributeSet
showDocumentProperties(PrinterJob job,
Window owner,
PrintService service,
PrintRequestAttributeSet aset) {
if (!(job instanceof WPrinterJob)) {
return null;
}
WPrinterJob wJob = (WPrinterJob)job;
return wJob.showDocumentProperties(owner, service, aset);
}
protected void updatePageAttributes(PrintService service,
PageFormat page) {
if (this.attributes == null) {
this.attributes = new HashPrintRequestAttributeSet();
}
updateAttributesWithPageFormat(service, page, this.attributes);
}
/**
* Associate this PrinterJob with a new PrintService.
*
* Throws <code>PrinterException</code> if the specified service
* cannot support the <code>Pageable</code> and
* </code>Printable</code> interfaces necessary to support 2D printing.
* @param a print service which supports 2D printing.
*
* @throws PrinterException if the specified service does not support
* 2D printing.
*/
@Override
public void setPrintService(PrintService service)
throws PrinterException {
super.setPrintService(service);
if (!(service instanceof Win32PrintService)) {
return;
}
driverDoesMultipleCopies = false;
driverDoesCollation = false;
setNativePrintServiceIfNeeded(service.getName());
}
public synchronized PrintService[] getPrintServices() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPrintJobAccess();
}
if (printServices == null) {
refreshServices();
}
return printServices;
}
private List<MediaTray> loadAvailableTrays(PrintService printService){
mediaTrays = new ArrayList<MediaTray>();
Object attributes =
printService.getSupportedAttributeValues(Media.class,
DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);
if (attributes != null && attributes.getClass().isArray()) {
for (Media media : (Media[]) attributes) {
if (media instanceof MediaTray) {
mediaTrays.add((MediaTray) media);
}
}
}
return mediaTrays;
}
public synchronized PrintService getPrintServiceByName(String name) {
if (name == null || name.equals("")) {
return null;
} else {
/* getPrintServices() is now very fast. */
PrintService[] printServices = getPrintServices();
for (int i=0; i<printServices.length; i++) {
if (printServices[i].getName().equals(name)) {
return printServices[i];
}
}
return null;
}
}
synchronized PrinterGraphicsConfig getPrinterGraphicsConfig() {
if (pgConfig != null) {
return pgConfig;
}
String deviceID = "Printer Device";
PrintService service = getPrintService();
if (service != null) {
deviceID = service.toString();
}
pgConfig = new PrinterGraphicsConfig(deviceID,
defaultDeviceTransform,
deviceWidth, deviceHeight);
return pgConfig;
}
private static List<ServiceItem> collectPrinterList() {
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
List<ServiceItem> list = new ArrayList<>(printServices.length);
for (PrintService service : printServices) {
list.add(new ServiceItem(service.getName()));
}
return list;
}
public synchronized PrintService getDefaultPrintService() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPrintJobAccess();
}
// Windows does not have notification for a change in default
// so we always get the latest.
defaultPrinter = getDefaultPrinterName();
if (defaultPrinter == null) {
return null;
}
if ((defaultPrintService != null) &&
defaultPrintService.getName().equals(defaultPrinter)) {
return defaultPrintService;
}
// Not the same as default so proceed to get new PrintService.
// clear defaultPrintService
defaultPrintService = null;
if (printServices != null) {
for (int j=0; j<printServices.length; j++) {
if (defaultPrinter.equals(printServices[j].getName())) {
defaultPrintService = printServices[j];
break;
}
}
}
if (defaultPrintService == null) {
defaultPrintService = new Win32PrintService(defaultPrinter);
}
return defaultPrintService;
}
static void trial() {
PrintService pserv1 = PrintServiceLookup.lookupDefaultPrintService();
PrintService[] pservs = PrintServiceLookup.lookupPrintServices(null, null);
PrintService pserv2 = PrintServiceLookup.lookupDefaultPrintService();
if ((pserv1 == null) || (pserv2==null)) {
return;
}
if (pserv1.hashCode() != pserv2.hashCode()) {
throw new RuntimeException("Different hashCodes for equal print "
+ "services: " + pserv1.hashCode() + " "
+ pserv2.hashCode());
}
}
protected void updatePageAttributes(PrintService service,
PageFormat page) {
if (this.attributes == null) {
this.attributes = new HashPrintRequestAttributeSet();
}
updateAttributesWithPageFormat(service, page, this.attributes);
}
private PrintService getNamedPrinterNameSysV(String name) {
String command = "/usr/bin/lpstat -v " + name;
String []result = execCmd(command);
if (result == null || result[0].indexOf("unknown printer") > 0) {
return null;
} else {
return new UnixPrintService(name);
}
}
public synchronized PrintService getDefaultPrintService() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPrintJobAccess();
}
// Windows does not have notification for a change in default
// so we always get the latest.
defaultPrinter = getDefaultPrinterName();
if (defaultPrinter == null) {
return null;
}
if ((defaultPrintService != null) &&
defaultPrintService.getName().equals(defaultPrinter)) {
return defaultPrintService;
}
// Not the same as default so proceed to get new PrintService.
// clear defaultPrintService
defaultPrintService = null;
if (printServices != null) {
for (int j=0; j<printServices.length; j++) {
if (defaultPrinter.equals(printServices[j].getName())) {
defaultPrintService = printServices[j];
break;
}
}
}
if (defaultPrintService == null) {
defaultPrintService = new Win32PrintService(defaultPrinter);
}
return defaultPrintService;
}