I need to print a file that is stored on a local disk, but with such parameters: the number of pages, the choice of color / black and white print. Here is what I have:

Desktop.getDesktop().print(new File(file_path)); 

The problem is that this way the entire file is printed on the default printer (black and white printing).

I found something related to printing in the java.awt.print , but how to create a file object that I need to print can not understand.

  • Specify which files you want to print? - zRrr
  • txt, docx, pdf - Roma Heshten
  • Look directly at the library, such as pdfbox , docx4j . - zRrr

1 answer 1

Found such a way to print and add attributes:

 PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(new PageRanges(1,5)); PrintService[] services = PrintServiceLookup.lookupPrintServices(myFormat, aset); if (services.length > 0) { DocPrintJob job = services[0].createPrintJob(); try { job.print(myDoc, aset); } catch (PrintException pe) {} }