How to print a pdf file in a specific tray without user interaction in java

I'm trying to set up a service that runs at night to automatically print a bunch of invoices and other documents to a bunch of printers. At the moment, I can print the documents in order, but I need to specify the tray (one with our letterhead and one with white paper). Everything that I tried so far does not work at all, I specify the MediaTray attribute in the PrintRequestAttribute set, but it does nothing. Has anyone had experience with something like this?

My current code that I use for testing is as follows.

// Create a PDFFile from a File reference File f = new File("C:\\File.pdf"); FileInputStream fis = new FileInputStream(f); FileChannel fc = fis.getChannel(); ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page PDFPrintPage pages = new PDFPrintPage(pdfFile); // Create Print Job PrinterJob pjob = PrinterJob.getPrinterJob(); PageFormat pf = PrinterJob.getPrinterJob().defaultPage(); pjob.setJobName(f.getName()); Book book = new Book(); book.append(pages, pf, pdfFile.getNumPages()); pjob.setPageable(book); // Send print job to default printer PrintRequestAttributeSet aset=new HashPrintRequestAttributeSet(); aset.add(MediaTray.MIDDLE); //Used several of the tray options here pjob.print(aset); 
+7
source share
2 answers

I use the jasper report. Here is the code.

 public void runReport() { JasperReport jasperReport; JasperPrint jasperPrint; try { jasperReport = JasperCompileManager.compileReport("C:/temp/jtest.jrxml"); jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), new JREmptyDataSource()); PrinterJob job = PrinterJob.getPrinterJob(); /* Create an array of PrintServices */ PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); PageFormat pf = PrinterJob.getPrinterJob().defaultPage(); job.defaultPage(pf); int selectedService = 0; String theUserPrinterName = "\\\\office1\\printer1"; AttributeSet attrSet = new HashPrintServiceAttributeSet(new PrinterName(theUserPrinterName, null)); services = PrintServiceLookup.lookupPrintServices(null, attrSet); try { job.setPrintService(services[selectedService]); } catch (Exception e) { e.printStackTrace(); } PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(MediaSizeName.NA_LETTER); printRequestAttributeSet.add(new Copies(1)); exporter = new JRPrintServiceExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); /* We set the selected service and pass it as a paramenter */ exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, services[selectedService]); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[selectedService].getAttributes()); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE); exporter.exportReport(); } catch (JRException e) { System.out.println("Caught exception!!!"); e.printStackTrace(); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE); try { exporter.exportReport(); } catch (JRException e2) { e2.printStackTrace(); } } 
+2
source

what do you actually use to print pdf? Sending PDF directly to the printer only works if the printer supports PDF. Otherwise, you need to rasterize the Java library. There is a blog article that offers ways to print PDFs from JAva at http://www.jpedal.org/PDFblog/2010/01/printing-pdf-files-from-java/

0
source

All Articles