Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending plain text to the default printer

Tags:

java

printing

I'm trying to send a simple string ("Hello world") to the printer, but for some reason, it only prints the first character ("H")

Here's the code

public class CardPrinter {
    public static void main(String[] args) {
        try {
            PrintService mPrinter = null;
            Boolean bFoundPrinter = false;

            PrintService[] printServices = PrinterJob.lookupPrintServices();

            //
            // Iterates the print services and print out its name.
            //
            for (PrintService printService : printServices) {
                String sPrinterName = printService.getName();
                if (sPrinterName.equals("DTC4000 Card Printer")) {
                    mPrinter = printService;
                    bFoundPrinter = true;
                }
            }

            // Open the image file
            String testData = "Hello World !";
            InputStream is = new ByteArrayInputStream(testData.getBytes());
            DocFlavor flavor =  DocFlavor.INPUT_STREAM.AUTOSENSE   ;

            // Find the default service
            PrintService service = PrintServiceLookup.lookupDefaultPrintService();
            System.out.println(service);

            // Create the print job
            DocPrintJob job = service.createPrintJob();
            Doc doc= new SimpleDoc(is, flavor, null);

            // Monitor print job events; for the implementation of PrintJobWatcher,
            PrintJobWatcher pjDone = new PrintJobWatcher(job);

            // Print it
            job.print(doc, null);

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

            // It is now safe to close the input stream
            is.close();
        } catch (PrintException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class PrintJobWatcher {
        // true iff it is safe to close the print job's input stream
        boolean done = false;

        PrintJobWatcher(DocPrintJob job) {
            // Add a listener to the print job
            job.addPrintJobListener(new PrintJobAdapter() {
                public void printJobCanceled(PrintJobEvent pje) {
                    allDone();
                }
                public void printJobCompleted(PrintJobEvent pje) {
                    allDone();
                }
                public void printJobFailed(PrintJobEvent pje) {
                    allDone();
                }
                public void printJobNoMoreEvents(PrintJobEvent pje) {
                    allDone();
                }
                void allDone() {
                    synchronized (PrintJobWatcher.this) {
                        done = true;
                        PrintJobWatcher.this.notify();
                    }
                }
            });
        }
        public synchronized void waitForDone() {
            try {
                while (!done) {
                    wait();
                }
            } catch (InterruptedException e) {
            }
        }
    }
}

Any ideas?

like image 627
Shai Avatar asked Aug 16 '12 14:08

Shai


People also ask

What does a default printer mean?

The default printer is where your print jobs will automatically be sent when using Quick Print option in MS Office applications and others. It is also the printer that is automatically selected in all print dialog.

How do I select a default printer?

To choose a default printer: Select Start > Settings . Go to Bluetooth & devices > Printers & scanners > select a printer. Then select Set as default.

How do I change the default printer in Word?

Besides, in the MS Word's Menu bar, click Tools > Option. Then choose the Printer tab. On the default paper tray option, choose Use Default Printer Setting.

How do I change the default printer on my Iphone?

Select the device for which you wish to change the default printer and, using your thumb or finger, swipe left, revealing an Orange Pencil Icon. Tap it and you will be able to set the default printer for this device.


1 Answers

Adding a form feed,

String testData = "Hello World !\f";

Solved the problem

like image 137
Shai Avatar answered Oct 21 '22 22:10

Shai