Results 1 to 4 of 4
  1. #1
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default plain text printing with java

    Hi all,

    I'm trying to make a program which prints out to a dot-matrix printer. This means I cant send the printer a picture to print - but some plain text and command codes.

    I've been looking on the internet for a while and have had no success. Every piece of code I've found so far takes a string and turns it into a picture to send to the windows spooler.

    My dad keeps telling me its easy in Basic, and I refuse to allow him to gloat about Java being worse than Basic due to my inability to print plain text!

    It doesnt help that I dont have the actual printer to test on :rolleyes:
    I'm sending the printout to the Generic/Text only printer on windows, but I dont think this should be affecting anything (my dads Basic programs work fine this way)

    Thanks,
    Berkeleybross

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Do the printers have Windows printer drivers? If so, then you would print to it same as any other Windows printer. If not, then perhaps you need to send data directly to the parallel port and use the printer's own codes (escape codes?). I'm not quite sure how this is done though.

    At least you can ask your dad to make a complex GUI program with Basic that runs on Windows, Unix and Mac.

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Im currently sending printouts to the Generic / Text only printer which comes with windows, and this is being sent to the FILE: port.

    If i use a program such as Notepad++ sending them to this printer comes out properly i.e. opening the resulting file has intelligble words in it.
    However in java, it gets converted to an image and sent to the printer - which comes out as garbage.
    And I cant send it directly to a parallel port, as it has to (eventually) sent to a network printer.

    Berkeleybross

  4. #4
    teknopaul is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default simple solution

    I know this is late but this thread comes up in google so I thought Id supply a fix after a few hours of fiddling.

    I found I could print text and printer control chars with the javax.print APIs, not the java.awt APIs, like this...
    Java Code:
    byte[] bytes = getSomeAsciiChars();
    DocFlavor byteFlavor = DocFlavor.BYTE_ARRAY.TEXT_PLAIN_US_ASCII;
    Doc bytesDoc = new SimpleDoc(bytes, byteFlavor, null);		
    PrintServiceAttributeSet aset = new HashPrintServiceAttributeSet();
    aset.add(new PrinterName(printerName, null));  
    PrintService[] services = PrintServiceLookup.lookupPrintServices(byteFlavor, aset);
    if (services.length > 0) {
    	DocPrintJob job = services[0].createPrintJob();
    	try {
    		job.print(bytesDoc, printAtts);
    	} catch (PrintException pe) {
    		log.error("", pe);
    	}
    }
    I set up a RAW print queue and used that print queue as the name for "printerName".

    To find out what printer names Java can see use PrintServiceLookup.lookupPrintServices(null, null);


    The only control chars I needed were [10,13] for new lines and 12 to spit the page out at the end. Clearly since this method is printer specific your mileage may vary.

Similar Threads

  1. Replies: 3
    Last Post: 06-08-2010, 08:10 PM
  2. text printing service in java
    By rameshkondur in forum AWT / Swing
    Replies: 1
    Last Post: 04-08-2009, 03:56 AM
  3. Indentation - plain text
    By barney75 in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 05:54 PM
  4. Java Text Printing
    By vivekn in forum Introductions
    Replies: 0
    Last Post: 03-13-2009, 08:05 AM
  5. convert html to plain text
    By vissu007 in forum New To Java
    Replies: 3
    Last Post: 07-07-2007, 02:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •