public Print2DPrinterJob() { /* Construct the print request specification. * The print data is a Printable object. * the request additonally specifies a job name, 2 copies, and * landscape orientation of the media. */ PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(OrientationRequested.LANDSCAPE); aset.add(new Copies(2)); aset.add(new JobName("My job", null)); /* Create a print job */ PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintable(this); /* locate a print service that can handle the request */ PrintService[] services = PrinterJob.lookupPrintServices(); if (services.length > 0) { System.out.println("selected printer " + services[0].getName()); try { pj.setPrintService(services[0]); pj.pageDialog(aset); if(pj.printDialog(aset)) { pj.print(aset); } } catch (PrinterException pe) { System.err.println(pe); } } }
/** * Show a dialog for printing the current drawing. * * @param fff the parent frame which will be used for dialogs and message boxes. * @param CCr the CircuitPanel containing the drawing to be exported. */ public void printDrawing(JFrame fff, CircuitPanel CCr) { cc = CCr; DialogPrint dp = new DialogPrint(fff); dp.setMirror(printMirror); dp.setFit(printFitToPage); dp.setBW(printBlackWhite); dp.setLandscape(printLandscape); dp.setVisible(true); // Get some information about the printing options. printMirror = dp.getMirror(); printFitToPage = dp.getFit(); printLandscape = dp.getLandscape(); printBlackWhite = dp.getBW(); Vector<LayerDesc> ol = cc.dmp.getLayers(); if (dp.shouldPrint()) { if (printBlackWhite) { Vector<LayerDesc> v = new Vector<LayerDesc>(); // Here we create an alternative array of layers in // which all colors are pitch black. This may be // useful for PCB's. for (int i = 0; i < LayerDesc.MAX_LAYERS; ++i) v.add( new LayerDesc( new ColorSwing(Color.black), ((LayerDesc) ol.get(i)).getVisible(), "B/W", ((LayerDesc) ol.get(i)).getAlpha())); cc.dmp.setLayers(v); } PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); // Set the correct printing orientation. if (printLandscape) { aset.add(OrientationRequested.LANDSCAPE); } else { aset.add(OrientationRequested.PORTRAIT); } job.print(aset); } catch (PrinterException ex) { // The job did not successfully complete JOptionPane.showMessageDialog(fff, Globals.messages.getString("Print_uncomplete")); } } cc.dmp.setLayers(ol); } }
public static void exporterPapier(JasperPrint jPrint, Integer nbCopies) { // choix de l'imprimante par défaut PrintService service = PrintServiceLookup.lookupDefaultPrintService(); // paramètres d'impression PrintRequestAttributeSet printRequestAttributeSetImpression = new HashPrintRequestAttributeSet(); printRequestAttributeSetImpression.add(MediaSizeName.ISO_A4); printRequestAttributeSetImpression.add(new Copies(nbCopies)); // paramètres de l'imprimante PrintServiceAttributeSet printServiceAttributeSetImpression = new HashPrintServiceAttributeSet(); printServiceAttributeSetImpression.add(new PrinterName(service.getName(), null)); // création de l'interface d'impression JRPrintServiceExporter imprimanteFacture = new JRPrintServiceExporter(); // initialisation de l'interface d'impression imprimanteFacture.setParameter(JRExporterParameter.JASPER_PRINT, jPrint); imprimanteFacture.setParameter( JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSetImpression); imprimanteFacture.setParameter( JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSetImpression); try { imprimanteFacture.exportReport(); } catch (Exception e) { throw new RuntimeException("Impossible de générer la Facture", e); // COMMANDE ENREGISTREE MAIS NON IMPRIMEE } }
public PrintService[] getPrintServices(DocFlavor flavor, AttributeSet attributes) { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPrintJobAccess(); } PrintRequestAttributeSet requestSet = null; PrintServiceAttributeSet serviceSet = null; if (attributes != null && !attributes.isEmpty()) { requestSet = new HashPrintRequestAttributeSet(); serviceSet = new HashPrintServiceAttributeSet(); Attribute[] attrs = attributes.toArray(); for (int i = 0; i < attrs.length; i++) { if (attrs[i] instanceof PrintRequestAttribute) { requestSet.add(attrs[i]); } else if (attrs[i] instanceof PrintServiceAttribute) { serviceSet.add(attrs[i]); } } } /* * Special case: If client is asking for a particular printer * (by name) then we can save time by getting just that service * to check against the rest of the specified attributes. */ PrintService[] services = null; if (serviceSet != null && serviceSet.get(PrinterName.class) != null) { PrinterName name = (PrinterName) serviceSet.get(PrinterName.class); PrintService service = getPrintServiceByName(name.getValue()); if (service == null || !matchingService(service, serviceSet)) { services = new PrintService[0]; } else { services = new PrintService[1]; services[0] = service; } } else { services = getPrintServices(); } if (services.length == 0) { return services; } else { ArrayList matchingServices = new ArrayList(); for (int i = 0; i < services.length; i++) { try { if (services[i].getUnsupportedAttributes(flavor, requestSet) == null) { matchingServices.add(services[i]); } } catch (IllegalArgumentException e) { } } services = new PrintService[matchingServices.size()]; return (PrintService[]) matchingServices.toArray(services); } }
private void defaultPrintPage(InvoicePagePrint pageprintdocumnet) { PrintService[] service = PrinterJob.lookupPrintServices(); // list // of // printers int count = service.length; PrintService printSvc = null; for (int i = 0; i < count; i++) { System.out.println("Service name: " + service[i].getName()); if (service[i].getName().indexOf("PDFcamp") != -1) { printSvc = service[i]; i = count; } } // TSP650 PageFormat format = new PageFormat(); Paper paper = new Paper(); double paperWidth = 3.25; double paperHeight = 11.69; double leftMargin = 0.19; double rightMargin = 0.25; double topMargin = 0; double bottomMargin = 0.01; paper.setSize(paperWidth * 72.0, paperHeight * 72.0); paper.setImageableArea( leftMargin * 72.0, topMargin * 72.0, (paperWidth - leftMargin - rightMargin) * 72.0, (paperHeight - topMargin - bottomMargin) * 72.0); format.setPaper(paper); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(OrientationRequested.PORTRAIT); PrinterJob printerJob = PrinterJob.getPrinterJob(); try { printerJob.setPrintService(printSvc); } catch (PrinterException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // Printable printable = new ReceiptPrintTest(); format = printerJob.validatePage(format); // printerJob.setPrintable(printable, format); printerJob.setPrintable(pageprintdocumnet, format); try { printerJob.print(aset); } catch (Exception e) { e.printStackTrace(); } }
private void print() { desktop.setWaitCursor(); hideCatchingRooms(); printDlg.setVisible(true); try { List<Module> modules = memberService.findModuleOrders(personFile.getId(), dates.getStart(), dates.getEnd()); StringBuilder sb = new StringBuilder(personFile.getContact().getFirstnameName()); if (modules != null && !modules.isEmpty()) { sb.append(" : "); sb.append(modules.get(0).getTitle()); if (modules.size() > 1) { sb.append("..."); // do not display next modules } } MessageFormat header = new MessageFormat(sb.toString()); MessageFormat footer = new MessageFormat("Page {0}"); PrintRequestAttributeSet prs = new HashPrintRequestAttributeSet(); prs.add(MediaSizeName.ISO_A4); prs.add(Sides.TWO_SIDED_LONG_EDGE); prs.add(OrientationRequested.PORTRAIT); prs.add( new JobName( BundleUtil.getLabel("Follow.up.label") + "-" + personFile.getId(), Locale.getDefault())); // 210 x 297 mm | 8.3 x 11.7 in 1 inch = 25.4 mm MediaPrintableArea printableArea = new MediaPrintableArea(10f, 10f, 190f, 277f, MediaPrintableArea.MM); prs.add(printableArea); printTable.print(JTable.PrintMode.FIT_WIDTH, header, footer, true, prs, true); } catch (PrinterException ex) { GemLogger.logException(ex); } finally { printDlg.setVisible(false); desktop.setDefaultCursor(); } }
/** * For any given paper, this retrieves the hardware margins, or a reasonable and safe guess if * they aren't available. */ public Rectangle2D printableArea(Paper paper) { int INCH = MediaSize.INCH; Rectangle2D area = null; MediaSizeName msn = MediaSize.findMedia( (float) paper.getWidth(), (float) paper.getHeight(), (int) (INCH / 72.0)); if (msn != null) { PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); pras.add(msn); MediaPrintableArea[] mpa = (MediaPrintableArea[]) service.getSupportedAttributeValues(MediaPrintableArea.class, null, pras); if (mpa != null && mpa.length > 0) { int MPA_INCH = MediaPrintableArea.INCH; area = new Rectangle2D( mpa[0].getX(MPA_INCH), mpa[0].getY(MPA_INCH), mpa[0].getWidth(MPA_INCH), mpa[0].getHeight(MPA_INCH)); } } // If we could not get the area for whatever reason, // then go with 0.75" margins unless they are too large // ie its a really small paper. if (area == null) { double pw = (paper.getWidth() / 72.0); ; double ph = (paper.getHeight() / 72.0); double iw, ih; if (pw < 3.0) { iw = 0.75 * pw; } else { iw = pw - 1.5; } if (ph < 3.0) { ih = 0.75 * ph; } else { ih = ph - 1.5; } double lm = (pw - iw) / 2.0; double tm = (ph - ih) / 2.0; area = new Rectangle2D(lm, tm, iw, ih); } return area; }
@Override public void actionPerformed(ActionEvent arg0) { // Background this, as it takes a while. JabRefExecutorService.INSTANCE.execute( () -> { try { PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); pras.add(new JobName(entry.map(BibEntry::getCiteKey).orElse("NO ENTRY"), null)); previewPane.print(null, null, true, null, pras, false); } catch (PrinterException e) { // Inform the user... we don't know what to do. JOptionPane.showMessageDialog( PreviewPanel.this, Localization.lang("Could not print preview") + ".\n" + e.getMessage(), Localization.lang("Print entry preview"), JOptionPane.ERROR_MESSAGE); LOGGER.info("Could not print preview", e); } }); }
/** * @param cont * @param pOrientation * @param paper * @param offscrnImageType if < 0 onscreen, otherwise integer BufferedImage type * @param dpi * @param numSamples multisampling value: < 0 turns off, == 0 leaves as-is, > 0 enables using * given num samples * @param tileWidth custom tile width for {@link TileRenderer#setTileSize(int, int, int) tile * renderer}, pass -1 for default. * @param tileHeight custom tile height for {@link TileRenderer#setTileSize(int, int, int) tile * renderer}, pass -1 for default. * @param resizeWithinPrintTest TODO */ public PrintableBase doPrintAuto( Container cont, int pOrientation, Paper paper, int offscrnImageType, int dpi, int numSamples, int tileWidth, int tileHeight, boolean resizeWithinPrintTest) { lock.lock(); try { final PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(MediaSizeName.ISO_A1); // 594 × 841 mm aset.add(MediaSizeName.ISO_A2); // 420 × 594 mm aset.add(MediaSizeName.ISO_A3); // 297 × 420 mm aset.add(MediaSizeName.ISO_A4); // 210 × 297 mm printCount++; final String psMimeType = "application/postscript"; final String pdfMimeType = "application/pdf"; final PrinterJob pj = PrinterJob.getPrinterJob(); StreamPrintServiceFactory[] factories = PrinterJob.lookupStreamPrintServices(pdfMimeType); if (factories.length > 0) { final String fname = getPrintFilename( offscrnImageType, dpi, numSamples, tileWidth, tileHeight, "pdf", resizeWithinPrintTest); System.err.println("doPrint: dpi " + dpi + ", " + fname); FileOutputStream outstream; try { outstream = new FileOutputStream(fname); return doPrintAutoImpl( cont, pj, factories[0].getPrintService(outstream), pOrientation, paper, offscrnImageType, dpi, numSamples, tileWidth, tileHeight, resizeWithinPrintTest); } catch (FileNotFoundException e) { Assert.assertNull("Unexpected exception", e); } } System.err.println("No PDF"); factories = PrinterJob.lookupStreamPrintServices(psMimeType); if (factories.length > 0) { final String fname = getPrintFilename( offscrnImageType, dpi, numSamples, tileWidth, tileHeight, "ps", resizeWithinPrintTest); System.err.println("doPrint: dpi " + dpi + ", " + fname); FileOutputStream outstream; try { outstream = new FileOutputStream(fname); return doPrintAutoImpl( cont, pj, factories[0].getPrintService(outstream), pOrientation, paper, offscrnImageType, dpi, numSamples, tileWidth, tileHeight, resizeWithinPrintTest); } catch (FileNotFoundException e) { Assert.assertNull("Unexpected exception", e); } } System.err.println("No PS"); return null; } finally { lock.unlock(); } }
public void printPage(String size) { double width = 0.0; double height = 0.0; double margin = 0.0; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); try { PrinterJob pj = PrinterJob.getPrinterJob(); if (pj.printDialog()) { PageFormat pf = pj.defaultPage(); Paper paper = pf.getPaper(); if (size.equals("3r")) { width = 5d * 72d; height = 3.5d * 72d; margin = 0d * 72d; aset.add(MediaSizeName.JIS_B7); } else if (size.equals("4r")) { width = 6d * 72d; height = 4d * 72d; margin = 0d * 72d; aset.add(MediaSizeName.JAPANESE_POSTCARD); } else if (size.equals("5r")) { width = 7d * 72d; height = 5d * 72d; margin = 0d * 72d; aset.add(MediaSizeName.NA_5X7); } else if (size.equals("6r")) { width = 8d * 72d; height = 6d * 72d; margin = 0d * 72d; aset.add(MediaSizeName.JAPANESE_DOUBLE_POSTCARD); } else if (size.equals("8r")) { width = 10d * 72d; height = 8d * 72d; margin = 0d * 72d; aset.add(MediaSizeName.NA_8X10); } paper.setSize(width, height); paper.setImageableArea(margin, margin, width - (margin * 2), height - (margin * 2)); System.out.println("Before- " + dump(paper)); pf.setOrientation(PageFormat.LANDSCAPE); pf.setPaper(paper); System.out.println("After- " + dump(paper)); System.out.println("After- " + dump(pf)); dump(pf); PageFormat validatePage = pj.validatePage(pf); System.out.println("Valid- " + dump(validatePage)); Book pBook = new Book(); pBook.append(this, pf); pj.setPageable(pBook); aset.add(OrientationRequested.LANDSCAPE); try { pj.setPrintable(this); pj.print(aset); } catch (PrinterException ex) { ex.printStackTrace(); } } } catch (Exception exp) { exp.printStackTrace(); } }
/* There's some inefficiency here as the job set is created even though * it may never be requested. */ private synchronized void initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet) { reqAttrSet = new HashPrintRequestAttributeSet(); jobAttrSet = new HashPrintJobAttributeSet(); Attribute[] attrs; if (reqSet != null) { reqAttrSet.addAll(reqSet); attrs = reqSet.toArray(); for (int i = 0; i < attrs.length; i++) { if (attrs[i] instanceof PrintJobAttribute) { jobAttrSet.add(attrs[i]); } } } DocAttributeSet docSet = doc.getAttributes(); if (docSet != null) { attrs = docSet.toArray(); for (int i = 0; i < attrs.length; i++) { if (attrs[i] instanceof PrintRequestAttribute) { reqAttrSet.add(attrs[i]); } if (attrs[i] instanceof PrintJobAttribute) { jobAttrSet.add(attrs[i]); } } } /* add the user name to the job */ String userName = ""; try { userName = System.getProperty("user.name"); } catch (SecurityException se) { } if (userName == null || userName.equals("")) { RequestingUserName ruName = (RequestingUserName) reqSet.get(RequestingUserName.class); if (ruName != null) { jobAttrSet.add(new JobOriginatingUserName(ruName.getValue(), ruName.getLocale())); } else { jobAttrSet.add(new JobOriginatingUserName("", null)); } } else { jobAttrSet.add(new JobOriginatingUserName(userName, null)); } /* if no job name supplied use doc name (if supplied), if none and * its a URL use that, else finally anything .. */ if (jobAttrSet.get(JobName.class) == null) { JobName jobName; if (docSet != null && docSet.get(DocumentName.class) != null) { DocumentName docName = (DocumentName) docSet.get(DocumentName.class); jobName = new JobName(docName.getValue(), docName.getLocale()); jobAttrSet.add(jobName); } else { String str = "JPS Job:" + doc; try { Object printData = doc.getPrintData(); if (printData instanceof URL) { str = ((URL) (doc.getPrintData())).toString(); } } catch (IOException e) { } jobName = new JobName(str, null); jobAttrSet.add(jobName); } } jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet); }