Java将txt文件发送到Printer

时间:2016-05-15 15:56:29

标签: java printing landscape

我制作了一个生成文本文件(.txt)的程序,然后在textArea中显示它们。我想更进一步,并能够打印文本文件。感觉就像我尝试了一切,但总能得到相同的结果。文本文件的长度总是不同,通常长度超过一页。使用我的文本文件下面的代码打印,但它不是打印多个页面,而是在横向单张纸张的六分之一上打印每页。我只是需要它才能正常打印。垂直在多个页面上。我在Linux环境中运行,它是一台Epson打印机。任何建议或反馈将不胜感激。

public static void tryDoc(){
    try{
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        pras.add(OrientationRequested.PORTRAIT);
        DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
        PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
        PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);

        if(service != null){
            DocPrintJob job = service.createPrintJob();
            FileInputStream input = new FileInputStream(FileName);
            DocAttributeSet das = new HashDocAttributeSet();
            Doc doc = new SimpleDoc(input,flavor,das);
            job.print(doc, pras);
        }
    }
    catch(Exception e){
        System.out.println("Failed");
    }
}

What printed page looks like

0 个答案:

没有答案