Ahmed Ali
Ahmed Ali

Reputation: 167

java printing code not working

I am using the java code shown below to print a text file on an HP DeskJet1000 USB printer attached to my computer. Whenever I run this code a printing job is sent but the printer does not print anything. The status shows that the printer is printing but it doesn't even intake a page. Please help! My code follows:

package printing;

import java.io.FileInputStream;
import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

/** @author Majid */
public class Printing {
    public static void main (String [] args) {
        // TODO code application logic here
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet ();
        /* locate a print service that can handle it */
        PrintService [] pservices = PrintServiceLookup.lookupPrintServices (flavor, aset);
        /* create a print job for the chosen service */
        int printnbr = 0;
        DocPrintJob pj = pservices [printnbr].createPrintJob ();
        try {
            FileInputStream fis = new FileInputStream ("e:/fypdatabase/test.txt");
            Doc doc = new SimpleDoc (fis, flavor, null);
            //PrintJobWatcher pjDone = new PrintJobWatcher (pj);
            /* print the doc as specified */
            pj.print (doc, aset);
        }
        catch (Exception ex) {
            ex.printStackTrace ();
        }  
    }
}

Upvotes: 5

Views: 4079

Answers (2)

lookus69
lookus69

Reputation: 11

@ moskiteau why you hard code number [2] in

DocPrintJob pj = pservices[2].createPrintJob();

instead of getting the value of printer as pservices' index?

DocPrintJob pj = pservices[printer].createPrintJob();

(im sorry if this isnt the right place to clarify this question, but this is my very first question here and didnt find how to ask this in any other way)

Upvotes: 1

moskiteau
moskiteau

Reputation: 1102

Your code is actually working. But probably you are trying to print to the wrong printer...

try this:

package printing;

import java.io.FileInputStream;
import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

/** @author Majid */
public class Printing {

    public static void main (String [] args) {
        // TODO code application logic here
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet ();
        /* locate a print service that can handle it */
        PrintService [] pservices = PrintServiceLookup.lookupPrintServices (flavor, aset);

        try {
            int printer = getPrinter(pservices);
            if(printer == -1) {
                throw new Exception("No network printer found");
            }
            DocPrintJob pj = pservices[2].createPrintJob();
            FileInputStream fis = new FileInputStream ("c:/Temp/test.txt");
            Doc doc = new SimpleDoc (fis, flavor, null);
            pj.print (doc, aset);
        }
        catch (Exception ex) {
            ex.printStackTrace ();
        } 
    }

    private int getPrinter(PrintService[] pservices) {
        int printer = -1;
        for(int i = 0; i<pservices.size(); i++) {
            if(pservices[i].getName().contains("\\\\")) {
                System.out.println("network printer: " + pservices[i].toString());   
                printer = i;
                break;
            }        
        }
        return printer;
    }
}

Upvotes: 0

Related Questions