Results 1 to 5 of 5
- 08-12-2009, 06:42 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
Printing Duplex and 4 Copies on HP Printer from Java
Hi All,
I was wondering if someone can provide me a working example of a
print class that allows me to print a PDF or Postscript file to a HP Laserjet printer.
So far I'm able to print the document, however the moment I try to specify
attributes like Duplex, Tray or Copies(2) I run into issues.
The document is still printed, but the attributes do not make any difference.
Did a lot of research on the web and JAVA API's and have found all kind of examples on how it should work. But I'm not able to get any of the examples
work with the attributes.
What am I doing wrong ???
- 08-16-2009, 06:35 PM #2
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
Extended search to all kind of other forums by asking the same questions.
Seems no one is able to provide me a simple example of a print class that
allows me to select printer attributes like Duplex, Copies, Tray and that actually
works.
Lots of samples based on the JAVA Print API, but none of these works.
What am I doing wrong .
Please help..
- 08-16-2009, 06:58 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Er, how are we supposed to tell what you're doing wrong?
- 08-16-2009, 07:26 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 4
- Rep Power
- 0
Hi dlorde,
Here is the code is use to test printing a Postscript or PDF file from
the filesystem to a printer (HP Laserjet 5M postscript)
Looks like I'm doing something very stupid cause I cannot get the attributes to work.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet ;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.CopiesSupported;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaTray;
import javax.print.attribute.standard.PrinterName;
import javax.print.attribute.standard.Sides;
public class SimplePrint {
public static void main(String[] args) throws IOException {
Date startTime = new Date();
Date endTime;
FileInputStream psStream = null;
{
try {
psStream = new FileInputStream(
"C:/sample.ps");
} catch (FileNotFoundException ffne) {
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.POSTSCRIPT;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
// aset.add(new Copies(2));
// aset.add(MediaTray.TOP);
PrintService[] services = PrintServiceLookup.lookupPrintServices(
psInFormat, aset);
if (services.length > 0) {
for (int i = 0; i < services.length; i++) {
// Print service name
// System.out.println(services[i].getName());
if (services[i].getName().contains("5M")) { // your selected
// printer
System.out.println("PRINTER FOUND "
+ services[i].getName());
// Then query and print the MEDIA it supports
Media med[] = (Media[]) services[i]
.getSupportedAttributeValues(Media.class, null,
null);
for (int k = 0; k < med.length; k++) {
System.out.println("MediaName : "
+ med[k].getClass() + " - Value : "
+ med[k].getValue() + " - String: "
+ med[k].toString().trim());
if (med[k].toString().trim().contains("manual")) {
System.out.println("Found Tray 1");
aset.add((Media) med[k]);
}
}
// Then query and print the COPIES it supports
CopiesSupported copSupp = (CopiesSupported) services[i]
.getSupportedAttributeValues(Copies.class,
null, null);
if (copSupp != null && copSupp.contains(2)) {
System.out
.println("Printer supports : CopiesRange = "
+ copSupp);
aset.add(new Copies(2));
System.out.println("Printer Copies now set to 2");
} else {
System.out
.println("Printer supports : CopiesRange = "
+ copSupp);
}
// Then query and print the document ATTRIBUTES it
// supports
for (Attribute pa : aset.toArray()) {
System.out.println("PrinterServiceAtt = " + pa);
}
// Then query and print the DOCUMENT TYPES it can print
DocFlavor[] flavors = services[i]
.getSupportedDocFlavors();
for (int j = 0; j < flavors.length; j++) {
// Filter out DocFlavors that have a representation
// class other
// than java.io.InputStream.
String repclass = flavors[j]
.getRepresentationClassName();
if (!repclass.equals("java.io.InputStream"))
continue;
System.out.println("PrinterFlavors = "
+ flavors[j].getMimeType());
}
DocPrintJob job = services[i].createPrintJob();
try {
job.print(myDoc, aset);
endTime = new Date();
System.out
.println("Printing Took - (ms) : "
+ (endTime.getTime() - startTime
.getTime()));
} catch (PrintException pe) {
System.out.println("PRINT ERROR");
}
}
}
} else
System.out.println("NO PRINTER FOUND !");
}
}
}
- 09-10-2009, 01:40 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
How to change paper source or printer tray of a printer in java code
By JAVA_ER in forum Advanced JavaReplies: 2Last Post: 02-20-2009, 07:25 AM -
printing simple text as text on printer
By Nicholas Jordan in forum Advanced JavaReplies: 0Last Post: 12-01-2008, 01:42 AM -
printer monitor in java
By sundarjothi in forum Advanced JavaReplies: 1Last Post: 06-14-2008, 02:52 AM -
Specifying a Network Printer in Printer Servlet
By shapez in forum New To JavaReplies: 0Last Post: 03-06-2008, 03:21 PM -
please help on java program for printer : this printer is connected to system which
By for453 in forum Java 2DReplies: 0Last Post: 08-09-2007, 06:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks