I have this method to do printing:
public void imprimir(String texto) {
PrintService[] printService = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, null);
PrintService impressoraPadrao = PrintServiceLookup.lookupDefaultPrintService();
DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
HashDocAttributeSet hashDocAttributeSet = new HashDocAttributeSet();
InputStream stream = new ByteArrayInputStream((texto + "\n").getBytes());
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());
}
}
}
No error occurs.
But it also does not print, and when I print to PDFCreator the page goes blank.
But if I send an image like this it prints normal:
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());
}
}
What am I doing wrong?