使用DocPrintJob打印

时间:2016-05-24 13:47:16

标签: java

我是巴西人,我不会说英语。遗憾...

我的代码:

PrintService[] printService = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, null);
PrintService impressoraPadrao = PrintServiceLookup.lookupDefaultPrintService();
DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
HashDocAttributeSet hashDocAttributeSet = new HashDocAttributeSet();

FileInputStream stream;
try {
    stream = new FileInputStream(new File("C:\\Users\\Rodrigo\\Desktop\\Trab4_Luc\\SCRIPTS.txt"));
    Doc doc = new SimpleDoc(stream, docFlavor, hashDocAttributeSet);
    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
    PrintService printServico = ServiceUI.printDialog(null, 300, 200, printService, impressoraPadrao, docFlavor, printRequestAttributeSet);
    if (printServico != null) {
        DocPrintJob docPrintJob = printServico.createPrintJob();

        try {
            docPrintJob.print(doc, printRequestAttributeSet);
        } catch (PrintException e) {
            JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
        }
    }
} catch (FileNotFoundException ex) {
    JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
}

此代码没有错误,但不会打印...

在记事本中直接打印工作正常...

但如果我发送图片打印正常

public void imprimir(String texto) {
    PrintService[] printService = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, null);
    PrintService impressoraPadrao = PrintServiceLookup.lookupDefaultPrintService();
    DocFlavor docFlavor = DocFlavor.INPUT_STREAM.PNG;
    HashDocAttributeSet hashDocAttributeSet = new HashDocAttributeSet();

    try {
         FileInputStream stream = new FileInputStream("C:\\Users\\Rodrigo\\Desktop\\Trab4_Luc\\teste.png");
         Doc doc = new SimpleDoc(stream, docFlavor, hashDocAttributeSet);
         PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
         PrintService printServico = ServiceUI.printDialog(null, 300, 200, printService, impressoraPadrao, docFlavor, printRequestAttributeSet);
         if (printServico != null) {
         DocPrintJob docPrintJob = printServico.createPrintJob();
         try {
             docPrintJob.print(doc, printRequestAttributeSet);
         } catch (PrintException e) {
             JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
         }
    } catch (FileNotFoundException ex) {
         JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
    }
}

我做错了什么?

0 个答案:

没有答案