将.txt文件发送到默认打印机

时间:2016-04-18 12:35:31

标签: java pdf printing

我试图打印一个.txt文件,它保存在我的电脑上。我拿起文件并将其发送到defaultPrintService

我在我的电脑上安装了“PDFCreator”作为默认打印机,但是当我打开生成的文件时,没有信息(只有白皮书)

我的代码

// check that the chose file Works - This works because I can see the information
    File file = null;
    file = new  File("c:/Tickets/ticket" + c.getId()
            + ".txt");
    FileInputStream fileSteam = new FileInputStream(file);
    BufferedReader reader = new BufferedReader(new InputStreamReader(fileSteam));
    String line="";
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }

已更改

//      BufferedReader reader = new BufferedReader(new InputStreamReader(fileSteam));
//      String line="";
//      while ((line = reader.readLine()) != null) {
//            System.out.println(line);
//        }

//创建打印文档

    DocFlavor docFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc document = new SimpleDoc(fileSteam, docFormat, null);

    PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();

    PrintService defaultPrintService = PrintServiceLookup
            .lookupDefaultPrintService(); 
if (defaultPrintService != null) {
            DocPrintJob printJob = defaultPrintService.createPrintJob();
            try {
                printJob.print(document, attributeSet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        System.err.println("No existen impresoras instaladas");
}fileSteam.close();

¿有人有想法因为信息不会出现在PDF上吗? 感谢所有

0 个答案:

没有答案