1
I have this method to make impression:
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());
}
}
}
There is no mistake.
But it also doesn’t print, and when I send the printing to Pdfcreator the page comes out blank.
But if I send a picture 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?
Someone to help?
– Rodrigo Lima