Boa tarde pessoal,
estou tentando implementar um sisteminhas de impressao…a impressora q. estou usando é uma bematech…
Minha intenção é imprimir um texto x…consegui imprimir…mas depois q. ele imprime corre um monte de papel…
tentei configurar o tamanho, mas nao tive sucesso…
segue abaixo meu código…quem puder analisar e dar uma sugestão…ficarei muuuuito grata!
public class GeradorImpressao {
private static GeradorImpressao instance = new GeradorImpressao();
private static PrintService printer = null;
private static PrintRequestAttributeSet aset = null;
private GeradorImpressao(){}
public static GeradorImpressao getInstance() {
return instance;
}
/**
* Efetua impress�o do texto passado como par�metro.
* @param texto
*/
public static void detectaImpressora(String toPrint, String printerName) {
aset = new HashPrintRequestAttributeSet();
aset.add(new JobName("Impressao", null));
aset.add(MediaSizeName.ISO_A8);
aset.add(new Copies(1));
// aset.add(MediaTray.MAIN);
for (PrintService p : PrinterJob.lookupPrintServices()) {
if (p.getName().equalsIgnoreCase(printerName)) {
printer = p;
break;
}
}
//MediaPrintableArea printableArea = (MediaPrintableArea)printer.getSupportedAttributeValues(MediaPrintableArea.class, null, aset);
try {
print(toPrint);
} catch (PrintException e) {
e.printStackTrace();
}
}
public synchronized static boolean print(String toPrint) throws PrintException {
try {
DocPrintJob docPrint = printer.createPrintJob();
PrintJobWatcher pjDone = new PrintJobWatcher(docPrint);
InputStream stream = new ByteArrayInputStream(toPrint.getBytes());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(stream, flavor, null);
docPrint.print(doc, aset);
pjDone.waitForDone();
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}
class PrintJobWatcher {
// true iff it is safe to close the print job's input stream
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}