0

我想从 java 打印文本文件,但后台处理程序消失了。如果我直接从记事本打印它就可以了。(我也尝试了 pdf 和图像,但它在 java 中不起作用)

我注意到来自 java 的数据类型是 RAW,但来自记事本的数据类型是 nt emf 1008

这是我的代码:

public void print(){
    String bon = "something to print"

    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter("D:/printer/bon.txt"));
    } catch (IOException ex) {
    }
    try {
        writer.write(bon);
        writer.close();
    } catch (IOException ex) {

    }
    FileInputStream textstream = null; 
    try { 
        textstream = new FileInputStream("D:/printer/bon.txt"); 
    } catch (FileNotFoundException ffne) { 
    } 
    if (textstream == null) { 
       return; 
    } 
    // Set the document type
    DocFlavor myFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
    // Create a Doc
    Doc myDoc = new SimpleDoc(textstream, myFormat, null); 
    // Build a set of attributes
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
    aset.add(new Copies(1)); 
    aset.add(MediaSizeName.ISO_A5);

    // discover the printers that can print the format according to the
    // instructions in the attribute set
    PrintService services =
            PrintServiceLookup.lookupDefaultPrintService();
    // Create a print job from one of the print services
    if (services!=null) { 
        System.out.println(services.getName());
        System.out.println(services.isDocFlavorSupported(myFormat));
        DocPrintJob job = services.createPrintJob(); 
        try { 

                job.print(myDoc, aset); 

                textstream.close();
        } catch (PrintException pe) {
            Logger.getLogger(pe.getMessage());
            System.out.println("error1");
        } catch (IOException ex) { 
            Logger.getLogger(Printing.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    else{
        System.out.println("printer tidak ada");
    }
}
4

0 回答 0