use of javax.print.PrintException in project jdk8u_jdk by JetBrains.
the class PSStreamPrintJob method pageableJob.
public void pageableJob(Pageable pageable, PrintRequestAttributeSet attributes) throws PrintException {
try {
synchronized (this) {
if (job != null) {
// shouldn't happen
throw new PrintException("already printing");
} else {
job = new PSPrinterJob();
}
}
job.setPrintService(getPrintService());
job.setPageable(pageable);
job.print(attributes);
notifyEvent(PrintJobEvent.JOB_COMPLETE);
return;
} catch (PrinterException pe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(pe);
} finally {
printReturned = true;
}
}
use of javax.print.PrintException in project jdk8u_jdk by JetBrains.
the class PSStreamPrintJob method printableJob.
public void printableJob(Printable printable, PrintRequestAttributeSet attributes) throws PrintException {
try {
synchronized (this) {
if (job != null) {
// shouldn't happen
throw new PrintException("already printing");
} else {
job = new PSPrinterJob();
}
}
job.setPrintService(getPrintService());
PageFormat pf = new PageFormat();
if (mediaSize != null) {
Paper p = new Paper();
p.setSize(mediaSize.getX(MediaSize.INCH) * 72.0, mediaSize.getY(MediaSize.INCH) * 72.0);
p.setImageableArea(72.0, 72.0, p.getWidth() - 144.0, p.getHeight() - 144.0);
pf.setPaper(p);
}
if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
pf.setOrientation(PageFormat.LANDSCAPE);
}
job.setPrintable(printable, pf);
job.print(attributes);
notifyEvent(PrintJobEvent.JOB_COMPLETE);
return;
} catch (PrinterException pe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(pe);
} finally {
printReturned = true;
}
}
use of javax.print.PrintException in project jdk8u_jdk by JetBrains.
the class PSStreamPrintJob method print.
public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException {
synchronized (this) {
if (printing) {
throw new PrintException("already printing");
} else {
printing = true;
}
}
this.doc = doc;
/* check if the parameters are valid before doing much processing */
DocFlavor flavor = doc.getDocFlavor();
Object data;
try {
data = doc.getPrintData();
} catch (IOException e) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException("can't get print data: " + e.toString());
}
if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintJobFlavorException("invalid flavor", flavor);
}
initializeAttributeSets(doc, attributes);
getAttributeValues(flavor);
String repClassName = flavor.getRepresentationClassName();
if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
try {
instream = doc.getStreamForBytes();
printableJob(new ImagePrinter(instream), reqAttrSet);
return;
} catch (ClassCastException cce) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(cce);
} catch (IOException ioe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(ioe);
}
} else if (flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.URL.JPEG) || flavor.equals(DocFlavor.URL.PNG)) {
try {
printableJob(new ImagePrinter((URL) data), reqAttrSet);
return;
} catch (ClassCastException cce) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(cce);
}
} else if (repClassName.equals("java.awt.print.Pageable")) {
try {
pageableJob((Pageable) doc.getPrintData(), reqAttrSet);
return;
} catch (ClassCastException cce) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(cce);
} catch (IOException ioe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(ioe);
}
} else if (repClassName.equals("java.awt.print.Printable")) {
try {
printableJob((Printable) doc.getPrintData(), reqAttrSet);
return;
} catch (ClassCastException cce) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(cce);
} catch (IOException ioe) {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException(ioe);
}
} else {
notifyEvent(PrintJobEvent.JOB_FAILED);
throw new PrintException("unrecognized class: " + repClassName);
}
}
use of javax.print.PrintException in project camel by apache.
the class PrinterOperations method print.
public void print(Doc doc, int copies, boolean sendToPrinter, String mimeType, String jobName) throws PrintException {
LOG.trace("Print Service: " + this.printService.getName());
LOG.trace("About to print " + copies + " copy(s)");
for (int i = 0; i < copies; i++) {
if (!sendToPrinter) {
LOG.debug("Print flag is set to false. This job will not be printed until this setting remains in effect." + " Please set the flag to true or remove the setting.");
File file;
if (mimeType.equalsIgnoreCase("GIF") || mimeType.equalsIgnoreCase("RENDERABLE_IMAGE")) {
file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".gif");
} else if (mimeType.equalsIgnoreCase("JPEG")) {
file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".jpeg");
} else if (mimeType.equalsIgnoreCase("PDF")) {
file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".pdf");
} else {
file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".txt");
}
LOG.debug("Writing print job to file: " + file.getAbsolutePath());
try {
InputStream in = doc.getStreamForBytes();
FileOutputStream fos = new FileOutputStream(file);
IOHelper.copyAndCloseInput(in, fos);
IOHelper.close(fos);
} catch (Exception e) {
throw new PrintException("Error writing Document to the target file " + file.getAbsolutePath());
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Issuing Job {} to Printer: {}", i, this.printService.getName());
}
print(doc, jobName);
}
}
}
use of javax.print.PrintException in project camel by apache.
the class PrinterProducer method assignPrintService.
private PrintService assignPrintService() throws PrintException {
PrintService printService;
if ((config.getHostname().equalsIgnoreCase("localhost")) && (config.getPrintername().equalsIgnoreCase("default"))) {
printService = PrintServiceLookup.lookupDefaultPrintService();
} else {
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
String name;
if (config.getHostname().equalsIgnoreCase("localhost")) {
// no hostname for localhost printers
name = config.getPrintername();
} else {
name = config.getHostname() + "/" + config.getPrintername();
if (config.getPrinterPrefix() != null) {
name = config.getPrinterPrefix() + name;
}
}
log.debug("Using printer name: {}", name);
setPrinter(name);
int position = findPrinter(services, printer);
if (position < 0) {
throw new PrintException("No printer found with name: " + printer + ". Please verify that the host and printer are registered and reachable from this machine.");
}
printService = services[position];
}
return printService;
}
Aggregations