Drukuj tekst Plik do określonej drukarki w Javie

Mam plik tekstowy i muszę go wydrukować na określonej drukarce sieciowej. Znam nazwę drukarki.

Do tej pory stworzyłem klasę Printable, aby wydrukować mój plik (bilet).

public class TicketPrintPage implements Printable {

    private File ticket;

    public TicketPrintPage(File f) {
        ticket = f;
    }

    public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
        int interline = 12;
        Graphics2D g2 = (Graphics2D) g;
        g2.setFont(new Font("CourierThai", Font.PLAIN, 10));
        int x =  (int) pf.getImageableX();
        int y = (int) pf.getImageableY();

        try {
            FileReader fr = new FileReader(ticket);
            BufferedReader br = new BufferedReader(fr);

            String s;
            while ((s = br.readLine()) != null) {
                y += interline;
                g2.drawString(s, x, y);
            }
        } catch (IOException e) {
            throw new PrinterException("File to print does not exist (" + ticket.getAbsolutePath() +") !");
        }
        return Printable.PAGE_EXISTS;
    }
}

Nazywam to TicketPrintPage w ten sposób:

public void printTicketFile(File ticket, int orientation) throws PrinterException {
    if (!ticket.exists()) {
        throw new PrinterException("Ticket to print does not exist (" + ticket.getAbsolutePath() + ") !");
    }
    PrinterJob pjob = PrinterJob.getPrinterJob();
    // get printer using PrintServiceLookup.lookupPrintServices(null, null) and looking at the name
    pjob.setPrintService(getPrintService());
    // job title
    pjob.setJobName(ticket.getName());

    // page fomat
    PageFormat pf = pjob.defaultPage();
    // landscape or portrait
    pf.setOrientation(orientation);
    // Paper properties
    Paper a4Paper = new Paper();
    double paperWidth  =  8.26;
    double paperHeight = 11.69;
    double margin = 16;
    a4Paper.setSize(paperWidth * 72.0, paperHeight * 72.0);
    a4Paper.setImageableArea(
                margin,
                //0,
                margin,
                //0,
                a4Paper.getWidth()- 2 * margin,
                //a4Paper.getWidth(),
                a4Paper.getHeight()- 2 * margin
                //a4Paper.getHeight()
                ); // no margin = no scaling
    pf.setPaper(a4Paper);
    // Custom class that defines how to layout file text
    TicketPrintPage pages = new TicketPrintPage(ticket);
    // adding the page to a book
    Book book = new Book();
    book.append(pages, pf);
    // Adding the book to a printjob
    pjob.setPageable(book);
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        // No jobsheet (banner page, the page with user name, job name, date and whatnot)
    pras.add(JobSheets.NONE);
    // Printing
    pjob.print(pras);
}

Działa nie tak źle, ale:
- Nie pracuję dla więcej niż jednej strony tekstu (znalazłem kilka algorytmów, ale dobrze)
- Nie mogę się dowiedzieć, kiedy drukarka skończy drukować, a jeśli spróbuję wydrukować dwa lub więcej biletów z rzędu, drukarka zwróci komunikat Drukarka niegotowa.

Ponownie pojawia się pytanie: czy nie ma prostego sposobu na wydrukowanie pliku tekstowego na drukarce?

questionAnswers(2)

yourAnswerToTheQuestion