本文介绍了在java中将文本文件打印到特定打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,我需要将它打印到特定的网络打印机.我知道打印机的名称.

I have a text file, and I need to print it to a specific network printer. I know the name of the printer.

到目前为止,我已经制作了一个 Printable 类来打印我的文件(票).

Until now I have made a Printable class to print my file (ticket).

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;
    }
}

我这样称呼这个 TicketPrintPage :

I call this TicketPrintPage this way :

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);
}

效果还不错,但是:
- 我不能处理超过一页的文本(找到了一些算法但很好)
- 我无法知道打印机何时完成打印,如果我尝试连续打印两张或更多票,打印机将返回打印机未就绪消息.

It works not so bad but :
- I doesn't work for more than one page of text (found some algorithms for that but well)
- I can't get to know when the printer is done printing, and if I try printing two or more tickets in a row the printer will return a Printer not ready message.

那么问题又来了:难道没有一种简单的方法可以将文本文件打印到打印机吗?

So the question again is : Isn't there a simple way to print a text file to a printer ?

推荐答案

我不确定这是否能解决您的问题,但我使用以下内容打印文本文件

I'm not sure if this solves your problem but I use the following to print a text file

FileInputStream textStream;
textStream = new FileInputStream(FILE_NAME);

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc mydoc = new SimpleDoc(textStream, flavor, null);

   PrintService[] services = PrintServiceLookup.lookupPrintServices(
                flavor, aset);
   PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

   if(services.length == 0) {
       if(defaultService == null) {
             //no printer found

       } else {
            //print using default
            DocPrintJob job = defaultService.createPrintJob();
            job.print(mydoc, aset);

       }

    } else {

       //built in UI for printing you may not use this
       PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, aset);


        if (service != null)
        {
           DocPrintJob job = service.createPrintJob();
           job.print(mydoc, aset);
        }

    }

您可能不需要 ServiceUI,但我认为您可以使用 PrintService[] 服务来获取可用于打印的打印机列表.使用输入流和 Doc 类,您可以将文件打印到打印机.

You may not need the ServiceUI, but I think you could use PrintService[] services to get a list of printers available for printing. And using an input stream and the Doc class you can print a file to a printer.

这篇关于在java中将文本文件打印到特定打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:10