使用Java,打印机换行符无法正常工作

时间:2017-11-07 06:38:33

标签: java printing network-printers

以下是我用java打印字符串的代码

String string2 = "    RECEIPT    \n==============\nHeader2\nHeader 3\nLine 4 Goes Here\nLine 5 Goes Here\n============";

InputStream is = new ByteArrayInputStream(string2.getBytes());
DocFlavor flavor =  DocFlavor.INPUT_STREAM.AUTOSENSE   ;

// Find the default service
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(service);

// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc= new SimpleDoc(is, flavor, null);

PrintJobWatcher pjDone = new PrintJobWatcher(job);

// Print it
job.print(doc, null);

pjDone.waitForDone();

// It is now safe to close the input stream
is.close();

就像在纸上打印一样。

 RECEIPT    
        ============== 
                      Header2
                             Header 3
                                     Line 4 Goes Here
                                                     Line 5 Goes Here

使用不带String的inputStream时也会发生相同的情况。任何建议PLZ。并且需要解释这个DocFlavor如何工作。谢谢

1 个答案:

答案 0 :(得分:2)

您必须使用\r\n代替\r

有两个ASCII码\n"换行"。它告诉打印机移动到下一行。并且\r"回车"它将马车移动到行首。