Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to use java code to print with a network printer

/**
 * Retrieve the specified Print Service; will return null if not found.
 * @return
 */
public static PrintService findPrintService(String printerName) {

    PrintService service = null;
    
    // Get array of all print services - sort order NOT GUARANTEED!
    PrintService[] services = PrinterJob.lookupPrintServices();
    
    // Retrieve specified print service from the array
    for (int index = 0; service == null && index < services.length; index++) {
        
        if (services[index].getName().equalsIgnoreCase(printerName)) {

            service = services[index];
        }
    }

    // Return the print service
    return service;
}

/**
 * Retrieve a PrinterJob instance set with the PrinterService using the printerName.
 * 
 * @return
 * @throws Exception IllegalStateException if expected printer is not found.
 */
public static PrinterJob findPrinterJob(String printerName) throws Exception {

    // Retrieve the Printer Service
    PrintService printService = PrintUtility.findPrintService(printerName);

    // Validate the Printer Service
    if (printService == null) {

        throw new IllegalStateException("Unrecognized Printer Service "" + printerName + '"');
    }
    
    // Obtain a Printer Job instance.
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    
    // Set the Print Service.
    printerJob.setPrintService(printService);

    // Return Print Job
    return printerJob;
}

/**
 * Printer list does not necessarily refresh if you change the list of 
 * printers within the O/S; you can run this to refresh if necessary.
 */
public static void refreshSystemPrinterList() {

    Class[] classes = PrintServiceLookup.class.getDeclaredClasses();

    for (int i = 0; i < classes.length; i++) {

        if ("javax.print.PrintServiceLookup$Services".equals(classes[i].getName())) {

            sun.awt.AppContext.getAppContext().remove(classes[i]);
            break;
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: aws s3 client in java 
Java :: What is sleep() method 
Java :: java singleton design pattern 
Java :: iterade dict javacirpt 
Java :: android ussd code example 
Java :: how to use int 
Java :: test date in java 
Java :: Java TreeMap Example remove() 
Java :: data.sql not loading in springboot 
Java :: spring jpa group by query method 
Java :: java classes and objects 
Java :: throw keyword in java 
Java :: method in java 
Java :: rotate matrix in java 
Java :: annotation spring notnull 
Java :: final method java 
Java :: java to c++ converter 
Java :: number of digits program in java 
Java :: how to find a int 
Java :: java 8 stream add to list 
Java :: write ajva program to vheck if anumber is less than 20 and greater than 5. It generates the exception out of range otherwise. If the number is within the range , then it displays the square of that number. 
Java :: como pegar o valor de um campo de texto swing 
Sql :: mysql reset auto increment value 
Sql :: find column in all stored procedures sql server 
Sql :: hibernate keeps deleting tables 
Sql :: oracle show grants on table 
Sql :: mysql workbench download for iinux mint 19.3 
Sql :: how to check port number for postgresql 
Sql :: install mysql on mac 
Sql :: ubuntu stop mysql 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =