简体   繁体   English

将文件打印到java中的特定打印机

[英]Print text File to specific printer in java

I have a text file, and I need to print it to a specific network printer. 我有一个文本文件,我需要将其打印到特定的网络打印机。 I know the name of the printer. 我知道打印机的名称。

Until now I have made a Printable class to print my file (ticket). 到目前为止,我已经制作了一个Printable类来打印我的文件(票证)。

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

I call this TicketPrintPage this way : 我用这种方式调用这个TicketPrintPage:

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 ? 所以问题是:是不是有一种简单的方法将文本文件打印到打印机?

JTextComponent#print should do the trick: JTextComponent #print应该可以解决问题:

JTextPane jtp = new JTextPane();
jtp.setBackground(Color.white);
jtp.setText("text to print");
boolean show = true;
try {
    jtp.print(null, null, show, null, null, show);
} catch (java.awt.print.PrinterException ex) {
    ex.printStackTrace();
}

in this manner you can quickly print out even nice formatted text - just create a StyledDocument and attach it to JTextPane before printing. 通过这种方式,您可以快速打印出漂亮的格式化文本 - 只需创建一个StyledDocument并在打印前将其附加到JTextPane。

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

    }

You may not need the ServiceUI, but I think you could use PrintService[] services to get a list of printers available for printing. 您可能不需要ServiceUI,但我认为您可以使用PrintService []服务来获取可用于打印的打印机列表。 And using an input stream and the Doc class you can print a file to a printer. 使用输入流和Doc类,您可以将文件打印到打印机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Java 中打印到特定打印机(IPP URI) - Print to specific printer (IPP URI) in Java Java - 如何将PDF打印到特定的打印机? - Java - how to print the PDF to a specific printer? 在Java中的热敏打印机上打印文本和二维码 - print text and qr code on thermical printer in Java 使用 JPS(java 打印服务)在特定打印机上打印文档 - Print a doc on specific printer using JPS( java print service) 如何在不显示打印对话框的情况下使用特定的打印机(Microsoft打印为PDF)和特定文件的名称来打印JTable? - how to print JTable with a specific printer (Microsoft Print to PDF) and a specific file's name without displaying the print dialog? 打印机独立的方式来预览和打印Java中的常规文本 - Printer independent way to preview and print generic text in Java 从 java 中的文本文件打印包含特定模式的行 - print a line that contains a specific pattern from a text-file in java 如何将变量传递到.prn文件以在Java中的热敏打印机上打印 - how to pass variables to .prn file to print on thermal printer in java 如何在Java中直接在网络打印机上打印文件。在运行应用程序的系统中未配置打印机? - How to print a file directly on network printer in java.. Printer is not configured in system where application is running? java在文本文件中以彩色打印 - java print in colour in text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM