Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java printing code not working

Tags:

java

printing

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 ();
        }  
    }
}
like image 263
Ahmed Ali Avatar asked Nov 13 '22 06:11

Ahmed Ali


1 Answers

@ 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)

like image 86
lookus69 Avatar answered Nov 16 '22 02:11

lookus69