java - 将纯文本发送到默认打印机

标签 java printing

我正在尝试向打印机发送一个简单的字符串(“Hello world”),但由于某种原因,它只打印第一个字符(“H”)

这是代码

public class CardPrinter {
    public static void main(String[] args) {
        try {
            PrintService mPrinter = null;
            Boolean bFoundPrinter = false;

            PrintService[] printServices = PrinterJob.lookupPrintServices();

            //
            // Iterates the print services and print out its name.
            //
            for (PrintService printService : printServices) {
                String sPrinterName = printService.getName();
                if (sPrinterName.equals("DTC4000 Card Printer")) {
                    mPrinter = printService;
                    bFoundPrinter = true;
                }
            }

            // Open the image file
            String testData = "Hello World !";
            InputStream is = new ByteArrayInputStream(testData.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);

            // Monitor print job events; for the implementation of PrintJobWatcher,
            PrintJobWatcher pjDone = new PrintJobWatcher(job);

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

            // Wait for the print job to be done
            pjDone.waitForDone();

            // It is now safe to close the input stream
            is.close();
        } catch (PrintException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class PrintJobWatcher {
        // true iff it is safe to close the print job's input stream
        boolean done = false;

        PrintJobWatcher(DocPrintJob job) {
            // Add a listener to the print job
            job.addPrintJobListener(new PrintJobAdapter() {
                public void printJobCanceled(PrintJobEvent pje) {
                    allDone();
                }
                public void printJobCompleted(PrintJobEvent pje) {
                    allDone();
                }
                public void printJobFailed(PrintJobEvent pje) {
                    allDone();
                }
                public void printJobNoMoreEvents(PrintJobEvent pje) {
                    allDone();
                }
                void allDone() {
                    synchronized (PrintJobWatcher.this) {
                        done = true;
                        PrintJobWatcher.this.notify();
                    }
                }
            });
        }
        public synchronized void waitForDone() {
            try {
                while (!done) {
                    wait();
                }
            } catch (InterruptedException e) {
            }
        }
    }
}

有什么想法吗?

最佳答案

添加表单提要,

String testData = "Hello World !\f";

解决了问题

关于java - 将纯文本发送到默认打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11988891/

相关文章:

java - 在 Windows 7 上的 solr 中创建 Maven Artifact 时遇到问题

java - 代号一 : Bad type on operand stack javax/swing/JScrollBar. getAccessibleContext()

java - 无法从java连接到打印机

php - 在javascript中打印页面

Java Swing 打印 - JTextPanel.Print

java - Pong 游戏运行速度太快,且带有 FPS 计数器

java - Hibernate:尝试保留对象图时出现 java.lang.StackOverflowError

java - 在黑莓中将日期转换为毫秒

c++ - 打印的 CDC 在纸上显得很小

javascript - `background-size: cover;` 的 CSS 打印修复