R
Rob@Bedford
I am working on a large-scale reporting solution. One component is to
be able to scan the network for all printers and maintain a record of
these printers. I have read many, many posts on these forums with
similar questions, most of which do not have any answers. Below is the
progress I have made so far, hopefully someone can offer some
input/insight.
1. Finding Network Printers - I used jcifs to browse the network and
find all shared devices that were printers.
Code:
private static void findAllPrinters() throws Exception {
SmbFile root = new SmbFile("smb://domain;USRASS@domain");
Hashtable printerHash = new Hashtable();
searchForPrinters(root, printerHash);
Enumeration keys = printerHash.keys();
System.out.println("Number of Printers Found: " +
printerHash.size());
SmbFile file = null;
while (keys.hasMoreElements()) {
file = ((SmbFile)keys.nextElement());
System.out.println("UNC: " + file.getUncPath());
}
}
private static void searchForPrinters(SmbFile root, Hashtable
printers) throws Exception {
SmbFile[] kids= null;
try {
kids = root.listFiles();
} catch (Exception e) {
}
if (kids == null)
return;
for (int i = 0; i < kids.length; i++) {
if (kids.getType() == SmbFile.TYPE_WORKGROUP ||
kids.getType() == SmbFile.TYPE_SERVER)
searchForPrinters(kids, printers);
else if (kids.getType() == SmbFile.TYPE_PRINTER) {
printers.put(kids, Boolean.TRUE);
}
}
}
By doing this I can find each printer on my network that I can find
when I use windows control panel and search for network printers.
2. I can print a test to any printer successfully.
Code:
FileOutputStream fos = new
FileOutputStream("\\\\PrintShare\\MyPrinter"); //hardcoded for test,
but taken from output of part #1 above.
PrintStream ps = new PrintStream(fos);
ps.print("\r");
ps.print("This page will be printed directly to the printer.");
ps.print("\f");
ps.close();
3. This is where i need help. Continuing on what I have already
accomplished...
A. How can I create a more complex printer reference to allow me to
control number of copies, color vs. bw, page layout, page size, obtain
details from printer such as acceptable page sizes, data types
printable, etc.
B Be able to detect printer errors and if the page has completed.
And and all input and/or collaboration is apprecaited! I feel I am
close to finding the link, but not sure where to find the missing
puzzle piece.
Thx,
Rob
be able to scan the network for all printers and maintain a record of
these printers. I have read many, many posts on these forums with
similar questions, most of which do not have any answers. Below is the
progress I have made so far, hopefully someone can offer some
input/insight.
1. Finding Network Printers - I used jcifs to browse the network and
find all shared devices that were printers.
Code:
private static void findAllPrinters() throws Exception {
SmbFile root = new SmbFile("smb://domain;USRASS@domain");
Hashtable printerHash = new Hashtable();
searchForPrinters(root, printerHash);
Enumeration keys = printerHash.keys();
System.out.println("Number of Printers Found: " +
printerHash.size());
SmbFile file = null;
while (keys.hasMoreElements()) {
file = ((SmbFile)keys.nextElement());
System.out.println("UNC: " + file.getUncPath());
}
}
private static void searchForPrinters(SmbFile root, Hashtable
printers) throws Exception {
SmbFile[] kids= null;
try {
kids = root.listFiles();
} catch (Exception e) {
}
if (kids == null)
return;
for (int i = 0; i < kids.length; i++) {
if (kids.getType() == SmbFile.TYPE_WORKGROUP ||
kids.getType() == SmbFile.TYPE_SERVER)
searchForPrinters(kids, printers);
else if (kids.getType() == SmbFile.TYPE_PRINTER) {
printers.put(kids, Boolean.TRUE);
}
}
}
By doing this I can find each printer on my network that I can find
when I use windows control panel and search for network printers.
2. I can print a test to any printer successfully.
Code:
FileOutputStream fos = new
FileOutputStream("\\\\PrintShare\\MyPrinter"); //hardcoded for test,
but taken from output of part #1 above.
PrintStream ps = new PrintStream(fos);
ps.print("\r");
ps.print("This page will be printed directly to the printer.");
ps.print("\f");
ps.close();
3. This is where i need help. Continuing on what I have already
accomplished...
A. How can I create a more complex printer reference to allow me to
control number of copies, color vs. bw, page layout, page size, obtain
details from printer such as acceptable page sizes, data types
printable, etc.
B Be able to detect printer errors and if the page has completed.
And and all input and/or collaboration is apprecaited! I feel I am
close to finding the link, but not sure where to find the missing
puzzle piece.
Thx,
Rob