Спулер исчезает на Java Печать
Я хочу напечатать текстовый файл из Java, но спулер исчез. Это работает, если я распечатаю его прямо из блокнота. (Я также пробовал PDF и изображения, но это не сработало из Java)
я заметил, что тип данных из Java является RAW, но тип данных из блокнота это NTF EMT 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");
}
}