gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:19:59 25 4
gpt4 key购买 nike

这是我用来使用 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 是如何工作的。谢谢

最佳答案

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

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

关于java - 使用 Java 打印机换行符无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47151471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com