2
votes

I want to print directly using a line printer i.e. a dot matrix printer using its features of font tab carriage return and line feed from my JAVA program. I basically know how to print from JAVA. My problem is that in JAVA printing we first generate the graphic image of the page to be printed and then send it to the printer to be printed. But I am not asking my question on those lines. I want to directly send the text as a stream of characters to the printer with the applicable commands for the printer for carriage return, Line feed, tabs and font of the printer just as in the old days when graphic printers like the laser or the inkjet printer were not in use.

I shall be very grateful if someone could guide me on these points. Thanks in advance.

Additional Info

Some of the comments are suggesting simple method of printing from a JTextComponent. Here we do not have to go through the task of creating the graphical printable which is automatically handled by the the JTextComponent, but my question is how to print without creating a graphical printable. Which means that first I select the font to use from the available fonts in my printer say "courier" and then I sent 'A' to the printer and the printer prints 'A' in "courier", then when I sent 'B' to the printer the printer prints 'B' in "courier" and so in till I change the selected font in my printer. Now at the end on the line, I sent \n for linefeed which will advance the roller drum of my printer by one line and \r for carriage return which will bring my printer's printing head to the beginning of the line.

For clarification I do not want to use printable interface, as the print method of this interface basically is used to generate a graphic image using the graphics object that is being passed as parameter to the print method. After this the JVM sends this graphics object to the printer to be printed as an image. This is not what I want. I want to use the line-printer's features of font and other commands.

2
I cannot figure out the process by which I could do it. I tried to google about my problem but all of the answers leads to graphical printing using java, which does not solve my problem.Blip
Have you tried JTextPane class's method setText("hello") and send it to printer using print() ?Sharp Edge
Well Neeraj the link you have sent is showing a simple method of printing from a JTextComponent. Here you do not have to go through the task of creating the graphical printable which is automatically handled by the the JTextComponent, but my question is how to print without creating a graphical printable. Which means that first I select the font to use from the available fonts in my printer and then I sent 'A' to the printer and the printer prints 'A' in my selected font and so on.Blip
Sharp edge, Your answer is similar to Neeraj's answer. I hope that I have able to clarify my problem in my previous comment.Blip

2 Answers

1
votes

This code doesn't require any Swing related component but still it needs Graphics class of awt, but you can print a text from console there is no UI component being displayed, just tested it:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PageRanges;


public class DirectPrint implements Printable {

private PrintService[] printService;
private String text;

public DirectPrint() {
    this.printService = PrinterJob.lookupPrintServices();
}

public static void main(String[] args) {
    DirectPrint lt = new DirectPrint();
    lt.printString("If this text gets printed, it will have worked! ;D");
}

public void printString(String input) {

    this.text = input;

    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new PageRanges(1, 1));
    aset.add(new Copies(1));

    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);

    try {
        printJob.setPrintService(getDefaultPrintService());
        //index of installed printers on you system
        //not sure if default-printer is always '0'
        printJob.print(aset);
    } catch (PrinterException err) {
        System.err.println(err);
    }
}

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    Graphics2D g2 = (Graphics2D) g;
    g2.translate(pf.getImageableX(), pf.getImageableY());
    g.drawString(String.valueOf(this.text), 14, 14);
    return PAGE_EXISTS;
  }
}

The method getDefaultPrintService() may return null, depending on your system.

Source: CodeRanch

** EDIT **

After further clarification, using the code below, there is no Graphic object being involved.

  InputStream in = null;
try {
log.debug("preparing input stream");
in = getFileTobePrinted();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

// find the printing service
log.debug("fetching print service");
AttributeSet attributeSet = new HashAttributeSet();
attributeSet.add(new PrinterName("lq2170", null));
PrintService[] services = PrintServiceLookup.lookupPrintServices(null,  attributeSet);

// create the print job
log.debug("creating print job");
DocPrintJob job = services[0].createPrintJob();
Doc doc = new SimpleDoc(in, flavor, null);

// monitor print job events
log.debug("preparing print job monitor");
PrintJobWatcher watcher = new PrintJobWatcher(job);

// print it
log.debug("start printing");
job.print(doc, null);

// wait for the print job is done
log.debug("waiting for the printing to finish");
watcher.waitForDone();

log.debug("done !");
  } finally {
if (in != null) try { in.close(); } catch(Exception e) {}
}

Found Here

1
votes

Have you tried to use This? but the rtextpr jar is Demo version and later you need to pay for licensed version.