Results 1 to 3 of 3
  1. #1
    pjmorce is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default Java Print API - Pages per sheet

    Hello

    I made a java application to print a file.
    Everything works fine.
    Now, I would like to be able to indicate that I want to print 2 pages per sheet.
    How can I do that?

  2. #2
    pjmorce is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    Ok, in theory i found my solution, but it is not working.

    I want to print 1 page in each side of a sheet.

    Here is the code I wrote to test it. The document is printed but with one page in each sheet instead of to be printed of both sides.

    How can I solve the problem?

    Thanks


    import java.io.FileInputStream;

    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.SimpleDoc;
    import javax.print.attribute.DocAttributeSet;
    import javax.print.attribute.HashAttributeSet;
    import javax.print.attribute.HashDocAttributeSet;
    import javax.print.attribute.HashPrintRequestAttributeSet ;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.PrinterName;
    import javax.print.attribute.standard.Sides;

    public class Tst1 {

    public static void main(String[] args) {
    FileInputStream fis = null;

    try {
    // Getting the PrintServices by Printername
    PrintService[] pservices =
    PrintServiceLookup.lookupPrintServices(null, new HashAttributeSet(
    new PrinterName("printername",null)));

    // Fetching first Printer of returned List.
    DocPrintJob pj = pservices[0].createPrintJob();

    // Create a PrintRequestAttributeSet
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

    // Add an PrintRequestAttribute to the PrintRequestAttributeSet
    pras.add(Sides.DUPLEX);

    fis = new FileInputStream("D:/1.pdf");

    DocAttributeSet das = new HashDocAttributeSet();
    Doc doc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, das);

    // Start Printing of the Document with PrintRequestAttributes
    pj.print(doc, pras);

    } catch(Exception e) {
    e.printStackTrace();
    } finally {
    try {
    fis.close();
    } catch(Exception e) {}
    }


    }

    }


  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Do you know for certain that pservices[0] supports double sided printing?

Similar Threads

  1. Java Server Pages
    By ninian in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-20-2008, 03:49 AM
  2. Java Server Pages (II)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 03-12-2008, 07:03 PM
  3. Java Server Pages (I)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 02-17-2008, 12:17 PM
  4. Replies: 0
    Last Post: 08-02-2007, 01:31 PM

Posting Permissions

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