Results 1 to 5 of 5
  1. #1
    Bozz is offline Member
    Join Date
    Aug 2009
    Rep Power

    Unhappy 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 ???

  2. #2
    Bozz is offline Member
    Join Date
    Aug 2009
    Rep Power


    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

    Lots of samples based on the JAVA Print API, but none of these works.

    What am I doing wrong .

    Please help..

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    Er, how are we supposed to tell what you're doing wrong?

  4. #4
    Bozz is offline Member
    Join Date
    Aug 2009
    Rep Power


    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.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(
    } catch (FileNotFoundException ffne) {
    if (psStream == null) {

    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,
    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]
    null, null);
    if (copSupp != null && copSupp.contains(2)) {
    .println("Printer supports : CopiesRange = "
    + copSupp);
    aset.add(new Copies(2));
    System.out.println("Printer Copies now set to 2");
    } else {
    .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]
    for (int j = 0; j < flavors.length; j++) {
    // Filter out DocFlavors that have a representation
    // class other
    // than
    String repclass = flavors[j]
    if (!repclass.equals(""))
    System.out.println("PrinterFlavors = "
    + flavors[j].getMimeType());

    DocPrintJob job = services[i].createPrintJob();
    try {
    job.print(myDoc, aset);
    endTime = new Date();
    .println("Printing Took - (ms) : "
    + (endTime.getTime() - startTime
    } catch (PrintException pe) {
    System.out.println("PRINT ERROR");

    } else
    System.out.println("NO PRINTER FOUND !");


  5. #5
    Iyyam is offline Member
    Join Date
    Sep 2009
    Rep Power


    Hi BOZZ,

    I also having this smae issue. I tried all the solutions which is taken from google. But i couldn't make it work.

    If you come to know the solution for this. Please post it here.

    Thanks you

Similar Threads

  1. Replies: 2
    Last Post: 02-20-2009, 08:25 AM
  2. printing simple text as text on printer
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2008, 02:42 AM
  3. printer monitor in java
    By sundarjothi in forum Advanced Java
    Replies: 1
    Last Post: 06-14-2008, 03:52 AM
  4. Specifying a Network Printer in Printer Servlet
    By shapez in forum New To Java
    Replies: 0
    Last Post: 03-06-2008, 04:21 PM
  5. Replies: 0
    Last Post: 08-09-2007, 07:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts