Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java将文本文件打印到特定打印机_Java_File_Printing - Fatal编程技术网

用java将文本文件打印到特定打印机

用java将文本文件打印到特定打印机,java,file,printing,Java,File,Printing,我有一个文本文件,需要将其打印到特定的网络打印机。我知道打印机的名字 到目前为止,我已经制作了一个可打印类来打印我的文件(票证) 我这样称呼这个TicketPrintPage: public void printTicketFile(File ticket, int orientation) throws PrinterException { if (!ticket.exists()) { throw new PrinterException("Ticket to pri

我有一个文本文件,需要将其打印到特定的网络打印机。我知道打印机的名字

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

我这样称呼这个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);
}
它的效果并不差,但:
-我只处理一页文字(找到了一些算法,但很好)
-我无法知道打印机何时完成打印,如果我尝试连续打印两张或更多票据,打印机将返回打印机未就绪消息


所以问题是:难道没有一种简单的方法可以将文本文件打印到打印机上吗?

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

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类,您可以将文件打印到打印机上

应该做到这一点:

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

通过这种方式,您甚至可以快速打印出格式良好的文本——只需创建一个StyledDocument并在打印前将其附加到JTextPane即可

这允许我打印页面(我没有使用printDialog),但不考虑PrintRequestAttributeSet。我要的是风景画,画的是肖像画。。。我想这是因为我使用了PrintServiceLookup.lookupPrintServices(null,null)。如果我指定了一个风格或属性,我就不会得到打印机列表。可能是因为它们是网络打印机,我不能用这种方式与它们交互?可能是因为我在网络打印方面也遇到了一些问题。因为我只需要打印一个文件(许可协议),所以我对文本文件进行了格式化,这样就不需要干扰打印设置。我没有任何尝试更改网络打印机设置的经验。对不起,aset是做什么用的?我试图运行您的代码,但遇到不可用的aset变量错误;我再也没有原始代码了。但是它应该是一个来自JavaSE7docs()的注释,作为对答案的补充,“这个方法在打印完成之前会被阻塞。”
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();
}