0

I am trying to print using javax. A simple print job is always printed as double sided even though the javax defaults to single sided, plus the printer is setup as 1 page per sheet.

The printer is HP 2015DN and here is my code:

String filename = "test.txt";
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

PrintService printService[] = PrintServiceLookup.lookupPrintServices(
    flavor, pras);

PrintService defaultService = PrintServiceLookup
    .lookupDefaultPrintService();

PrintService service = ServiceUI.printDialog(null, 200, 200,
    printService, defaultService, DocFlavor.INPUT_STREAM.AUTOSENSE, pras);

if (service != null) {
  DocPrintJob job = service.createPrintJob();
  FileInputStream fis = new FileInputStream(filename);
  DocAttributeSet das = new HashDocAttributeSet();
  Doc doc = new SimpleDoc(fis, flavor, das);
  pras.add(new Copies(1));
  job.print(doc, pras);
  fis.close();
}

I know that the printer can print single-sided because Notepad++ is able to do so..

Any help would be appreciated.. Thanks..

arin
  • 1,774
  • 22
  • 35
  • I'd recommend checking out the following for a more thorough treatment of the issue: [Printing with Attributes(Tray Control, Duplex, etc...) using javax.print library](http://stackoverflow.com/questions/14328012/printing-with-attributestray-control-duplex-etc-using-javax-print-library) – amaidment Sep 30 '15 at 07:47

2 Answers2

1

The printer configuration seemed to be the problem in this case; the printer was configured to print double-sided no matter the instructions within the Java API. Trying the code with a different printer helped understand the issue.

arin
  • 1,774
  • 22
  • 35
1

Maybe this will help:

pras.add(Sides.ONE_SIDED);
user802421
  • 7,465
  • 5
  • 40
  • 63
  • Unfortunately that does not seem to help. If I add it before the look-up, the printer is not found, if I add it after the look-up, the job is still printed double sided. – arin Jul 25 '11 at 14:25