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

Printer Line break not works properly using Java

这是我用来使用 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

使用不带字符串的 inputStream 时也会发生同样的情况。任何建议请。并且需要解释这个 DocFlavor working.Thanks

您必须使用 \r\n 而不是 \r

有两个ASCII码\n"line feed"。它告诉打印机移动到下一行。 \r "carriage return" 它将回车移动到行首。