public static void printDocument() throws IOException, PrinterException { PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); pras.add(Sides.TWO_SIDED_SHORT_EDGE); PDDocument input = PDDocument.load(new File("Karteikarten.pdf")); PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(input)); if (job.printDialog(pras)) { job.print(pras); } }
@Override public Class<?>[] getSupportedAttributeCategories(){ ArrayList<Class> categList = new ArrayList<Class>(otherAttrCats.length+3); for (int i=0; i < otherAttrCats.length; i++) { categList.add(otherAttrCats[i]); } if (settings.get_CanDuplex()) { categList.add(Sides.class); } if (settings.get_PrinterResolutions().get_Count() > 0) { categList.add(PrinterResolution.class); } return categList.toArray(new Class[categList.size()]); }
public Sides assignSides(String sidesString) { Sides answer; if (sidesString == null) { // default to one side if no slides configured answer = Sides.ONE_SIDED; } else if (sidesString.equalsIgnoreCase("one-sided")) { answer = Sides.ONE_SIDED; } else if (sidesString.equalsIgnoreCase("duplex")) { answer = Sides.DUPLEX; } else if (sidesString.equalsIgnoreCase("tumble")) { answer = Sides.TUMBLE; } else if (sidesString.equalsIgnoreCase("two-sided-short-edge")) { answer = Sides.TWO_SIDED_SHORT_EDGE; } else if (sidesString.equalsIgnoreCase("two-sided-long-edge")) { answer = Sides.TWO_SIDED_LONG_EDGE; } else { answer = Sides.ONE_SIDED; } return answer; }
@Test public void printToMiddleTray() throws Exception { PrinterEndpoint endpoint = new PrinterEndpoint(); PrinterConfiguration configuration = new PrinterConfiguration(); configuration.setHostname("localhost"); configuration.setPort(631); configuration.setPrintername("DefaultPrinter"); configuration.setMediaSizeName(MediaSizeName.ISO_A4); configuration.setInternalSides(Sides.ONE_SIDED); configuration.setInternalOrientation(OrientationRequested.PORTRAIT); configuration.setMediaTray("middle"); PrinterProducer producer = new PrinterProducer(endpoint, configuration); producer.start(); PrinterOperations printerOperations = producer.getPrinterOperations(); PrintRequestAttributeSet attributeSet = printerOperations.getPrintRequestAttributeSet(); Attribute attribute = attributeSet.get(javax.print.attribute.standard.Media.class); assertNotNull(attribute); assertTrue(attribute instanceof MediaTray); MediaTray mediaTray = (MediaTray) attribute; assertEquals("middle", mediaTray.toString()); }
@Test public void printsWithLandscapeOrientation() throws Exception { PrinterEndpoint endpoint = new PrinterEndpoint(); PrinterConfiguration configuration = new PrinterConfiguration(); configuration.setHostname("localhost"); configuration.setPort(631); configuration.setPrintername("DefaultPrinter"); configuration.setMediaSizeName(MediaSizeName.ISO_A4); configuration.setInternalSides(Sides.ONE_SIDED); configuration.setInternalOrientation(OrientationRequested.REVERSE_LANDSCAPE); configuration.setMediaTray("middle"); configuration.setSendToPrinter(false); PrinterProducer producer = new PrinterProducer(endpoint, configuration); producer.start(); PrinterOperations printerOperations = producer.getPrinterOperations(); PrintRequestAttributeSet attributeSet = printerOperations.getPrintRequestAttributeSet(); Attribute attribute = attributeSet.get(OrientationRequested.class); assertNotNull(attribute); assertEquals("reverse-landscape", attribute.toString()); }
public void setAttribute(final Attribute attr) { final Class<? extends Attribute> category = attr.getCategory(); if (OrientationRequested.class.equals(category)) { setOrientation((OrientationRequested) attr); } else if (MediaSize.class.equals(category)) { setPaper((MediaSize) attr); } else if (Media.class.equals(category)) { setPaper((MediaSizeName) attr); } else if (Paper.class.equals(category)) { setPaper((Paper) attr); } else if (Copies.class.equals(category)) { setCopies((Copies) attr); } else if (PrintQuality.class.equals(category)) { setPrintQuality((PrintQuality) attr); } else if (Sides.class.equals(category)) { setSides((Sides) attr); } else if (SheetCollate.class.equals(category)) { setCollate((SheetCollate) attr); } else if (PrinterResolution.class.equals(category)) { setPrinterResolution((PrinterResolution) attr); } else if (Chromaticity.class.equals(category)) { setChromaticity((Chromaticity) attr); } }
void fillSidesFields() { Sides side = (Sides) attrs.get(Sides.class); if (firstUse) { selectSidesButton(side); } Sides [] aList = (myService.isAttributeCategorySupported(Sides.class) ? (Sides []) (myService.getSupportedAttributeValues( Sides.class, flavor, attrs)) : null); enableSidesButtons(aList); if ((aList != null) && !sideIsEnabled(getSelectedSide())) { selectSidesButton(sideIsEnabled(side) ? side : (Sides) (myService.getDefaultAttributeValue(Sides.class))); } }
private void enableSidesButtons(Sides [] list) { oneSideBtn.setEnabled(false); duplexBtn.setEnabled(false); tumbleBtn.setEnabled(false); if (list != null) { for (int i = 0; i < list.length; i++) { if (list[i].equals(Sides.ONE_SIDED)) { oneSideBtn.setEnabled(true); } else if (list[i].equals(Sides.DUPLEX) || list[i].equals(Sides.TWO_SIDED_LONG_EDGE)) { duplexBtn.setEnabled(true); } else if (list[i].equals(Sides.TUMBLE) || list[i].equals(Sides.TWO_SIDED_SHORT_EDGE)) { tumbleBtn.setEnabled(true); } } } }
private void createSidesCombo(PrintRequestAttributeSet set) { Sides[] sides = (Sides[]) mService.getSupportedAttributeValues(Sides.class, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null); if (sides != null && sides.length > 0) { HashSet<Sides> possible = new HashSet<>(); for (Sides one : sides) { possible.add(one); } ArrayList<PageSides> choices = new ArrayList<>(); for (PageSides side : PageSides.values()) { if (possible.contains(side.getSides())) { choices.add(side); } } mSides = new JComboBox<>(choices.toArray(new PageSides[0])); mSides.setSelectedItem(PrintUtilities.getSides(mService, set)); UIUtilities.setOnlySize(mSides, mSides.getPreferredSize()); LinkedLabel label = new LinkedLabel(SIDES, mSides); add(label, new PrecisionLayoutData().setEndHorizontalAlignment()); add(mSides); } else { mSides = null; } }
@Test public void printsWithLandscapeOrientation() throws Exception { PrinterEndpoint endpoint = new PrinterEndpoint(); PrinterConfiguration configuration = new PrinterConfiguration(); configuration.setHostname("localhost"); configuration.setPort(631); configuration.setPrintername("DefaultPrinter"); configuration.setMediaSizeName(MediaSizeName.ISO_A4); configuration.setInternalSides(Sides.ONE_SIDED); configuration.setInternalOrientation(OrientationRequested.REVERSE_LANDSCAPE); configuration.setMediaTray("middle"); configuration.setSendToPrinter(false); PrinterProducer producer = new PrinterProducer(endpoint, configuration); producer.start(); PrinterOperations printerOperations = producer.getPrinterOperations(); PrintRequestAttributeSet attributeSet = printerOperations.getPrintRequestAttributeSet(); Attribute attribute = attributeSet.get(OrientationRequested.class); Assert.assertNotNull(attribute); Assert.assertEquals("reverse-landscape", attribute.toString()); }
public Class<?>[] getSupportedAttributeCategories() { ArrayList categList = new ArrayList(otherAttrCats.length+3); for (int i=0; i < otherAttrCats.length; i++) { categList.add(otherAttrCats[i]); } int caps = getPrinterCapabilities(); if ((caps & DEVCAP_DUPLEX) != 0) { categList.add(Sides.class); } if ((caps & DEVCAP_QUALITY) != 0) { int[] defaults = getDefaultPrinterSettings(); // Added check: if supported, we should be able to get the default. if ((defaults[3] >= DMRES_HIGH) && (defaults[3] < 0)) { categList.add(PrintQuality.class); } } PrinterResolution[] supportedRes = getPrintResolutions(); if ((supportedRes!=null) && (supportedRes.length>0)) { categList.add(PrinterResolution.class); } return (Class[])categList.toArray(new Class[categList.size()]); }
private void setSidesAttrib(Attribute attr) { if (attr == Sides.TWO_SIDED_LONG_EDGE) { mAttSides = 2; // DMDUP_VERTICAL } else if (attr == Sides.TWO_SIDED_SHORT_EDGE) { mAttSides = 3; // DMDUP_HORIZONTAL } else { // Sides.ONE_SIDED mAttSides = 1; } }
@Override final public void actionPerformed(ActionEvent objPactionEvent) { final Object objLsourceObject = objPactionEvent.getSource(); if (this.objGoneSidePrintIconRadioJButton.isSameAs(objLsourceObject)) { this.objGprintJDialog.getAttributes().add(Sides.ONE_SIDED); } else if (this.objGtumblePrintIconRadioJButton.isSameAs(objLsourceObject)) { this.objGprintJDialog.getAttributes().add(Sides.TUMBLE); } else if (this.objGduplexPrintIconRadioJButton.isSameAs(objLsourceObject)) { this.objGprintJDialog.getAttributes().add(Sides.DUPLEX); } }
public Class<?>[] getSupportedAttributeCategories() { ArrayList<Class<?>> categList = new ArrayList<>(otherAttrCats.length+3); for (int i=0; i < otherAttrCats.length; i++) { categList.add(otherAttrCats[i]); } int caps = getPrinterCapabilities(); if ((caps & DEVCAP_DUPLEX) != 0) { categList.add(Sides.class); } if ((caps & DEVCAP_QUALITY) != 0) { int[] defaults = getDefaultPrinterSettings(); // Added check: if supported, we should be able to get the default. if ((defaults[3] >= DMRES_HIGH) && (defaults[3] < 0)) { categList.add(PrintQuality.class); } } PrinterResolution[] supportedRes = getPrintResolutions(); if ((supportedRes!=null) && (supportedRes.length>0)) { categList.add(PrinterResolution.class); } return categList.toArray(new Class<?>[categList.size()]); }
/** * * @param g Graphic class * @param pf page format * @param page page count * @return 1 * @throws PrinterException * * This methos print to pdf */ @Override public int print(Graphics g, PageFormat pf, int page) throws PrinterException { DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE; PrintRequestAttributeSet patts = new HashPrintRequestAttributeSet(); patts.add(Sides.DUPLEX); PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, patts); if (ps.length == 0) { // } //System.out.println("Available printers: " + Arrays.asList(ps)); PrintService myService = null; for (PrintService printService : ps) { if (printService.getName().equals("Your printer name")) { myService = printService; break; } } if (myService == null) { // } try (FileInputStream fis = new FileInputStream(file)) { Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null); DocPrintJob printJob = myService.createPrintJob(); printJob.print(pdfDoc, new HashPrintRequestAttributeSet()); } catch (Exception e) { e.printStackTrace(); throw new PrinterException("File Not Found!"); } return 1; }
/** * Prints the given inputStream to the printer with the given name, or the default printer * if a printer with that name isn't found. * * @param inputStream Data stream (Usually PDF formatted) * @param printerName Destination printer name (case insensitive) * @param duplex Print the job double-sided */ void printDocument(InputStream inputStream, String printerName, boolean duplex) throws PrintException { javax.print.PrintService printService = findPrinter(printerName); DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; DocPrintJob job = printService.createPrintJob(); PrintRequestAttributeSet printRequestSet = new HashPrintRequestAttributeSet(); if (duplex) { printRequestSet.add(Sides.DUPLEX); } Doc doc = new SimpleDoc(inputStream, flavor, null); job.print(doc, printRequestSet); }
public void actionPerformed(ActionEvent e) { if (e.getSource() == calendar) atts.add(Sides.TWO_SIDED_SHORT_EDGE); else if (e.getSource() == oneside) atts.add(Sides.ONE_SIDED); else atts.add(Sides.TWO_SIDED_LONG_EDGE); }
/** * Called to update for new selected * print service. Tests if currently * selected attributes are supported. */ void updateForSelectedService() { if (categorySupported(Sides.class)) { oneside.setEnabled(true); calendar.setEnabled(true); duplex.setEnabled(true); Object defaultValue = defaultValue(Sides.class); Attribute sides = attribute(Sides.class); if (sides != null) { if (sides.equals(Sides.TWO_SIDED_SHORT_EDGE)) calendar.setSelected(true); else if (sides.equals(Sides.ONE_SIDED)) oneside.setSelected(true); else duplex.setSelected(true); } else { if (defaultValue.equals(Sides.TWO_SIDED_SHORT_EDGE)) calendar.setSelected(true); else if (defaultValue.equals(Sides.ONE_SIDED)) oneside.setSelected(true); else duplex.setSelected(true); } } else { oneside.setEnabled(false); calendar.setEnabled(false); duplex.setEnabled(false); } }
public PrinterOperations() throws PrintException { printService = PrintServiceLookup.lookupDefaultPrintService(); if (printService == null) { throw new PrintException("Printer lookup failure. No default printer set up for this host"); } flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(new Copies(1)); printRequestAttributeSet.add(MediaSizeName.NA_LETTER); printRequestAttributeSet.add(Sides.ONE_SIDED); }