59 lines
2.6 KiB
Java
59 lines
2.6 KiB
Java
import javax.print.PrintService;
|
|
import javax.print.PrintServiceLookup;
|
|
import javax.print.attribute.AttributeSet;
|
|
import javax.print.attribute.HashAttributeSet;
|
|
import javax.print.attribute.standard.PrinterName;
|
|
import java.util.Arrays;
|
|
|
|
public class ModularPrintTest {
|
|
public static void main(String[] args) throws Exception {
|
|
System.out.println("=== Modular Print Test ===");
|
|
|
|
// Method 1: Get all print services
|
|
System.out.println("\n1. All Print Services:");
|
|
PrintService[] allServices = PrintServiceLookup.lookupPrintServices(null, null);
|
|
System.out.println("Found " + allServices.length + " service(s):");
|
|
for (PrintService service : allServices) {
|
|
System.out.println(" - " + service.getName());
|
|
}
|
|
|
|
// Method 2: Lookup by printer name
|
|
System.out.println("\n2. Looking for specific printers...");
|
|
String[] printerNames = {"Brother_HL_L2370DN_series", "Brother_MFC_J6520DW", "HL-L2370DN"};
|
|
|
|
for (String name : printerNames) {
|
|
AttributeSet attrs = new HashAttributeSet();
|
|
attrs.add(new PrinterName(name, null));
|
|
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attrs);
|
|
if (services.length > 0) {
|
|
System.out.println("Found printer '" + name + "': " + services[0].getName());
|
|
} else {
|
|
System.out.println("Printer '" + name + "' not found via PrintServiceLookup");
|
|
}
|
|
}
|
|
|
|
// Method 3: Default printer
|
|
System.out.println("\n3. Default Printer:");
|
|
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
|
|
if (defaultService != null) {
|
|
System.out.println("Default: " + defaultService.getName());
|
|
} else {
|
|
System.out.println("No default printer found");
|
|
}
|
|
|
|
// Method 4: Check print service factories
|
|
System.out.println("\n4. Print Service Lookup Services:");
|
|
Class<?> lookupClass = PrintServiceLookup.class;
|
|
try {
|
|
// Try to get lookup services via reflection if direct method not available
|
|
var method = lookupClass.getMethod("lookupServices",
|
|
Class.forName("javax.print.DocFlavor"),
|
|
Class.forName("javax.print.attribute.AttributeSet"));
|
|
Object[] lookups = (Object[]) method.invoke(null, null, null);
|
|
System.out.println("Lookup services found: " + lookups.length);
|
|
} catch (Exception e) {
|
|
System.out.println("Could not get lookup services: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|