I'm trying to print files through Java using Swing but the printer does not respond. The code gives no error or exception (apparent). Would anyone know what the problem is?
public static void main(String[] args) {
DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(docFlavor, null);
System.out.println("Quantas impressoras: " + printServices.length);
PrintService impressoraPadrao = PrintServiceLookup.lookupDefaultPrintService();
System.out.println("A impressora padrão é: " + impressoraPadrao.getName());
HashDocAttributeSet hashDocAttributeSet = new HashDocAttributeSet();
try {
FileInputStream fileInputStream = new FileInputStream("C:\Temp\dados.txt");
Doc doc = new SimpleDoc(fileInputStream, docFlavor, hashDocAttributeSet);
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
PrintService printServico = ServiceUI.printDialog(null, 300, 200, printServices, impressoraPadrao, docFlavor, printRequestAttributeSet);
if(printServico != null){
DocPrintJob docPrintJob = printServico.createPrintJob();
try{//mandar imprimir o documento
docPrintJob.print(doc, printRequestAttributeSet);
} catch (PrintException ex) {
Logger.getLogger(Impressora.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Impressora.class.getName()).log(Level.SEVERE, null, ex);
}
}