public Object getDefaultAttributeValue(Class category) { if (category.equals(JobName.class)) { return new JobName("Java GDI client print job", Locale.US); } else if (category.equals(RequestingUserName.class)) { return new RequestingUserName(System.getProperty("user.name"), Locale.US); } else if (category.equals(Destination.class)) { File file = new File(System.getProperty("user.dir") + File.separator + "output.prn"); return new Destination(file.toURI()); } else if (category.equals(SheetCollate.class)) { return SheetCollate.COLLATED; } else if (category.equals(Copies.class)) { return new Copies(1); } return null; }
String getDestination(PrintRequestAttributeSet attrs) throws PrintException { if (attrs != null) { if (attrs.containsKey(Destination.class)) { Destination destination = (Destination)attrs.get(Destination.class); if (!destination.getURI().getScheme().equals("file")) { throw new PrintException( "Only files supported as destinations."); } String file = destination.getURI().getPath(); if (file.startsWith("/")) { file = file.substring(1); } return file; } } return null; }
private Object[] getDefaultAttributeValueEx(Class category) { if (Destination.class.isAssignableFrom(category)) { return new Object[0]; } else if (RequestingUserName.class.isAssignableFrom(category)) { return new Object[] { new RequestingUserName( (String) AccessController .doPrivileged(new PrivilegedAction() { public Object run() { return System.getProperty("user.name"); } }), Locale.US) }; } else if (JobName.class.isAssignableFrom(category)) { return new Object[] { new JobName("Java print job", Locale.US) }; } else if (DocumentName.class.isAssignableFrom(category)) { return new Object[] { new DocumentName("Java print document", Locale.US) }; } return null; }
private Object[] getSupportedAttributeValuesEx(Class category, DocFlavor flavor) { if (Destination.class.isAssignableFrom(category)) { String ms = flavor.getMediaSubtype(); if (ms.equalsIgnoreCase("gif") || ms.equalsIgnoreCase("jpeg") || ms.equalsIgnoreCase("png") || ms.equalsIgnoreCase("postscript") || flavor.getClass() == DocFlavor.SERVICE_FORMATTED.class) { try { return new Object[] { new Destination(new URI( "file:///foo/bar")) }; } catch (URISyntaxException e) { // return empty array - values are not supported return new Object[0]; } } } else if (RequestingUserName.class.isAssignableFrom(category)) { return new Object[] { new RequestingUserName("I.A.Muser", Locale.US) }; } else if (JobName.class.isAssignableFrom(category)) { return new Object[] { new JobName("Foo print job", Locale.US) }; } else if (DocumentName.class.isAssignableFrom(category)) { return new Object[] { new DocumentName("Foo document", Locale.US) }; } return null; }
public static void main(String[] args) throws Exception { PrinterJob printerJob = PrinterJob.getPrinterJob(); printerJob.setPrintable((graphics, pageFormat, pageIndex) -> { if (pageIndex != 0) { return Printable.NO_SUCH_PAGE; } else { Shape shape = new Rectangle(110, 110, 10, 10); Rectangle rect = shape.getBounds(); BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration().createCompatibleImage(rect.width, rect.height, Transparency.BITMASK); graphics.drawImage(image, rect.x, rect.y, rect.width, rect.height, null); return Printable.PAGE_EXISTS; } }); File file = null; try { HashPrintRequestAttributeSet hashPrintRequestAttributeSet = new HashPrintRequestAttributeSet(); file = File.createTempFile("out", "ps"); file.deleteOnExit(); Destination destination = new Destination(file.toURI()); hashPrintRequestAttributeSet.add(destination); printerJob.print(hashPrintRequestAttributeSet); } finally { if (file != null) { file.delete(); } } }
/** * Starts the application. */ public static void main(java.lang.String[] args) { PrintDlgApp pd = new PrintDlgApp(); PrinterJob pj = PrinterJob.getPrinterJob(); System.out.println(pj); PrintRequestAttributeSet pSet = new HashPrintRequestAttributeSet(); pSet.add(new Copies(1)); //PageFormat pf = pj.pageDialog(pSet); PageFormat pf = new PageFormat(); System.out.println("Setting Printable...pf = "+pf); if (pf == null) { return; } pj.setPrintable(pd,pf); //try { pj.setPrintService(services[0]); } catch(Exception e) { e.printStackTrace(); } pSet.add(new Destination(new java.io.File("./out.prn").toURI())); System.out.println("open PrintDialog.."); for (int i=0; i<2; i++) { if (pj.printDialog(pSet)) { try { System.out.println("About to print the data ..."); pj.print(pSet); System.out.println("Printed"); } catch (PrinterException pe) { pe.printStackTrace(); } } } }
private static void printTest() { PrintService defService = null, service[] = null; HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet(); DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG; service = PrintServiceLookup.lookupPrintServices(flavor, null); defService = PrintServiceLookup.lookupDefaultPrintService(); if ((service == null) || (service.length == 0)) { throw new RuntimeException("No Printer services found"); } File f = new File("output.ps"); Destination d = new Destination(f.toURI()); prSet.add(d); if (defService != null) { System.out.println("isAttrCategory Supported? " + defService.isAttributeCategorySupported(Destination.class)); System.out.println("isAttrValue Supported? " + defService.isAttributeValueSupported(d, flavor, null)); } defService = ServiceUI.printDialog(null, 100, 100, service, defService, flavor, prSet); ServiceUI.printDialog(null, 100, 100, service, defService, DocFlavor.SERVICE_FORMATTED.PAGEABLE, new HashPrintRequestAttributeSet()); }
/** * Called to update for new selected * print service. Tests if currently * selected attributes are supported. */ void updateForSelectedService() { PrinterMakeAndModel att1 = getSelectedPrintService().getAttribute(PrinterMakeAndModel.class); typValue.setText(att1 == null ? "" : att1.getValue()); PrinterInfo att2 = getSelectedPrintService().getAttribute(PrinterInfo.class); infoValue.setText(att2 == null ? "" : att2.getValue()); PrinterIsAcceptingJobs att3 = getSelectedPrintService().getAttribute(PrinterIsAcceptingJobs.class); PrinterState att4 = getSelectedPrintService().getAttribute(PrinterState.class); String status = att4.toString(); if (att3 == PrinterIsAcceptingJobs.ACCEPTING_JOBS) status += " - " + getLocalizedString("lb.acceptingjobs"); else if (att3 == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) status += " - " + getLocalizedString("lb.notacceptingjobs"); statusValue.setText(status); if (categorySupported(Destination.class)) { fileRedirection_cb.setEnabled(false); } }
/** If a PDF printer is available print to it. * @exception PrinterException If a printer with the string "PDF" * cannot be found or if the job cannot be set to the PDF print * service or if there is another problem printing. */ protected void _printPDF() throws PrinterException { // Find something that will print to PDF boolean foundPDFPrinter = false; PrintService pdfPrintService = null; PrintService printServices[] = PrinterJob.lookupPrintServices(); for (int i = 0; i < printServices.length; i++) { if (printServices[i].getName().indexOf("PDF") != -1) { foundPDFPrinter = true; pdfPrintService = printServices[i]; } } if (pdfPrintService == null) { throw new PrinterException("Could not find a printer with the " + "string \"PDF\" in its name."); } PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintService(pdfPrintService); job.setPrintable(plot, job.defaultPage()); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); // This gets ignored, but let's try it anyway Destination destination = new Destination(new File("plot.pdf").toURI()); aset.add(destination); job.print(aset); if (foundPDFPrinter) { System.out .println("Plot printed from command line. " + "Under MacOSX, look for " + "~/Desktop/Java Printing.pdf"); } }
public Object getDefaultAttributeValue( final Class<? extends Attribute> category) { checkArgs(category, null); final DevmodeStructWrapper dm = getDefaultPrinterProps(); if (JobName.class.equals(category)) { return DEFAULT_JOB_NAME; } else if (RequestingUserName.class.equals(category)) { return new RequestingUserName(getSystemProperty("user.name"), //$NON-NLS-1$ null); } else if (Destination.class.equals(category)) { File file = new File(getSystemProperty("user.dir") //$NON-NLS-1$ + File.separator + "output.prn"); //$NON-NLS-1$ return new Destination(file.toURI()); } else if (OrientationRequested.class.equals(category)) { return dm.getOrientation(); } else if (Paper.class.equals(category)) { return getDefaultPaper(); } else if (Media.class.equals(category)) { return getDefaultPaper().getSize().getMediaSizeName(); } else if (MediaSize.class.equals(category)) { return getDefaultPaper().getSize(); } else if (PrintQuality.class.equals(category)) { return dm.getPrintQuality(); } else if (Sides.class.equals(category)) { return dm.getSides(); } else if (Copies.class.equals(category)) { return dm.getCopies(); } else if (SheetCollate.class.equals(category)) { return dm.getCollate(); } else if (PrinterResolution.class.equals(category)) { return dm.getPrinterResolution(); } else if (Chromaticity.class.equals(category)) { return dm.getChromaticity(); } return null; }
private String getDestinationPath(final PrintRequestAttributeSet attrs) throws PrintException { if (attrs != null) { final Destination dest = (Destination) attrs .get(Destination.class); return dest != null ? new File(dest.getURI()).getAbsolutePath() : null; } return null; }
public Class[] getSupportedAttributeCategories() { ArrayList supportedCategories = new ArrayList(); if (getCopiesSupported(serviceName) >= 1) { supportedCategories.add(Copies.class); } if (getSidesSupported(serviceName)) { supportedCategories.add(Sides.class); } if (getSupportedMediaSizeNames() != null) { supportedCategories.add(Media.class); } if (getResolutionsSupported(serviceName) != null) { supportedCategories.add(PrinterResolution.class); } if (getOrientationSupported(serviceName)) { supportedCategories.add(OrientationRequested.class); } if (getCollateSupported(serviceName)) { supportedCategories.add(SheetCollate.class); } supportedCategories.add(Chromaticity.class); supportedCategories.add(JobName.class); supportedCategories.add(RequestingUserName.class); supportedCategories.add(Destination.class); Class[] categories = new Class[supportedCategories.size()]; supportedCategories.toArray(categories); return categories; }
void filltoFileBox() { if (firstUse && attrs.containsKey(Destination.class)) { toFileBox.setSelected(true); } toFileBox.setEnabled(checkFilePermission(destPermission) && myService.isAttributeCategorySupported(Destination.class)); }
private void getSupportedAttributeCategoriesEx(ArrayList clazz) { if (!clazz.contains(Destination.class)) { clazz.add(Destination.class); } if (!clazz.contains(RequestingUserName.class)) { clazz.add(RequestingUserName.class); } if (!clazz.contains(JobName.class)) { clazz.add(JobName.class); } if (!clazz.contains(DocumentName.class)) { clazz.add(DocumentName.class); } }
private boolean[] isAttributeValueSupportedEx(Attribute avalue, DocFlavor flavor) { if (Destination.class.isAssignableFrom(avalue.getCategory())) { String ms = (flavor != null ? flavor.getMediaSubtype() : ""); Class cls = (flavor != null ? flavor.getClass() : null); if (ms.equalsIgnoreCase("gif") || ms.equalsIgnoreCase("jpeg") || ms.equalsIgnoreCase("png") || ms.equalsIgnoreCase("postscript") || flavor == null || cls == DocFlavor.SERVICE_FORMATTED.class) { if (!canPrintToFile()) { return new boolean[] { false }; } URI uri = ((Destination) avalue).getURI(); try { File file = new File(uri); if (file.isFile()) { if (file.canWrite()) { return new boolean[] { true }; } return new boolean[] { false }; } String path = file.getParent(); File parent = new File(path); if (parent.isDirectory()) { if (parent.canWrite()) { return new boolean[] { true }; } return new boolean[] { false }; } } catch (Exception e) { return new boolean[] { false }; } } } return null; }