How to send print to a printer that is not set as default?

1

Hello, I am creating a java application in which I send graphics to a printer. The problem I have is that if I do not put the printer as the default in windows, it does not send print to that printer.

I've seen that the name is sent to print to the printer, but I do not know how to implement it.

This is my code with which I currently print:

PrinterJob job = PrinterJob.getPrinterJob();

int numero = Integer.parseInt(SNumero.getValue().toString());

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(numero));
aset.add(new MediaPrintableArea(0, 0, 62, 29, MediaPrintableArea.MM));
aset.add(Chromaticity.COLOR);
aset.add(OrientationRequested.PORTRAIT);
                


job.setPrintable(new ObjetoDeImpresion());
job.setJobName("nombre_de_impresion");

try {
  job.print(aset);
} catch (PrinterException ex) {
  System.out.println(ex);
}
         

I try to send the name of the printer in the following way but it does not work:

String printerName = "Brother QL-800 (Copiar 1)";

aset.add(new PrinterName(printerName, null));

Does anyone know how to do it?

    
asked by Giovani Preciado Ortiz 21.08.2017 в 18:53
source

1 answer

1

Here I leave you an example function, which returns an object of type PrintService by entering a String with the name of the printer we are looking for.

public PrintService impresora(String nombre){
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); //Obtenemos los servicios de impresion del sistema 
    for (PrintService impresora : printServices){ //Recorremos el array de servicios de impresion
        if(impresora.getName().contentEquals(nombre)){ // Si el nombre del servicio es el mismo que el que buscamos
            return impresora; // Nos devuelve el servicio 
        }
    }
    return null;    // Si no lo encuentra nos devuelve un null
}

Here I leave several links to the documentation of java about the PrintServices

    
answered by 21.08.2017 / 19:17
source