1

I am working on a sample application to print file on network printer. But i m unable to get success. Guys please help me out to get rid this problem.



FileInputStream fis = new FileInputStream(file);
if (fis == null) {
System.out.print("No File");
return;
}
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

PrintService service = PrintServiceLookup.lookupDefaultPrintService();

aset.add(new PrinterName("ipp:\\witnw21va\ipp\ITDepartment-HP4050", null));
//PrintServiceAttributeSet aset = HashPrintAttributeSet();
PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);

if (service != null){
System.out.println("Default Printer: " + service.getName());

// Creating DocPrintJob
DocPrintJob job = service.createPrintJob();
try{
Doc doc = new SimpleDoc(fis,flavor,null);

PrintJobWatcher pjDone = new PrintJobWatcher(job);

job.print(doc,aset);

// Wait for the print job to be done
pjDone.waitForDone();

fis.close();
}

Many thanks.

user1127643
  • 169
  • 4
  • 12
  • 3
    Welcome to StackOverflow. As it is, you have posted badly formatted, incomplete code without any details as to how it is failing. Unfortunately there's not much anyone can offer besides guesses. – Brian Roach Feb 01 '12 at 15:55
  • 3
    Guys go easy on him its actually useful for someone looking for printing on network – codefreaK Oct 18 '15 at 06:10
  • where is the code of PrintJobWatcher class anyway? – gumuruh Oct 30 '21 at 04:04

1 Answers1

2

That code won't compile, because you have invalid escape sequences in the printer name:

new PrinterName("ipp:\\witnw21va\ipp\ITDepartment-HP4050", null)

The Java compiler thinks you are trying to write special characters like newline \n, and is confused by \w, \i etc in that string, which are not legal.

You need to escape each backslash to make it legal:

new PrinterName("ipp:\\\\witnw21va\\ipp\\ITDepartment-HP4050", null)

or change it if it should in fact be forward slashes

DNA
  • 42,007
  • 12
  • 107
  • 146