Java print code not working

I am using the java code below to print a text file on an HP DeskJet1000 USB printer connected to my computer. Whenever I run this code, a print job is sent, but the printer does not print anything. Status indicates that the printer is printing, but it does not even pick up the page. Please help! My code is:

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 ();
        }  
    }
}
+5
source share
2 answers

@moskiteau, why do you have a hard code number [2] in

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

instead of getting the printer value as a pservices index?

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

(I'm sorry if this is not the right place to clarify this question, but this is my first question here and did not find how to ask about it in any other way)

+1
source

. , , ...

:

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;
    }
}
0

All Articles