Jump to content
Changes to the Jaspersoft community edition download ×

Double Sided printing from Solaris platform


Recommended Posts

This is probably more of a JPS question, but perhaps someone has run across this. The below code is a short program that attempts to print a 2 page document much the way Jasper currently does. When compiled and run on my windows machine the document prints as expected when selecting any of the double sided options. However, when I move the program to a machine running Solaris printing to the same printer (a network printer) running the same version of Java, the document prints but regardless of what options I give it is always single sided.


Any ideas?


import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.*;
import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.lang.reflect.Method;

public class Print2DPrinterJob implements Printable {
private String printStyleLabel;

public Print2DPrinterJob(String printerName, String printStyle) {
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintServiceAttributeSet servAttSet = new HashPrintServiceAttributeSet();

if (printStyle.equalsIgnoreCase("Duplex"«»)) {
printStyleLabel = Sides.DUPLEX.toString();
} else if (printStyle.equalsIgnoreCase("Tumble"«»)) {
printStyleLabel = Sides.TUMBLE.toString();
} else if (printStyle.equalsIgnoreCase("twoSidedShortEdge"«»)) {
printStyleLabel = Sides.TWO_SIDED_SHORT_EDGE.toString();
} else if (printStyle.equalsIgnoreCase("twoSidedLongEdge"«»)) {
printStyleLabel = Sides.TWO_SIDED_LONG_EDGE.toString();
} else {
printStyleLabel = Sides.ONE_SIDED.toString();

aset.add(new Copies(1));
aset.add(new JobName("Jasper Test job", null));

servAttSet.add(new PrinterName(printerName, null));

/* Create a print job */
PrinterJob pj = PrinterJob.getPrinterJob();
/* locate a print service that can handle the request */
PrintService[] services =
PrintServiceLookup.lookupPrintServices(null, servAttSet);

if (services.length > 0) {
System.out.println("selected printer " + services[0].getName());
try {
} catch (PrinterException pe) {

public int print(Graphics g,PageFormat pf,int pageIndex) {

if (pageIndex < 2) {
Graphics2D g2d= (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.drawString("This is page " + (pageIndex + 1) + ", " + printStyleLabel, 250, 250);
g2d.fillRect(0, 0, 200, 200);
return Printable.PAGE_EXISTS;
} else {
return Printable.NO_SUCH_PAGE;

public static void initPrinterJobFields(PrinterJob job)
Class klass = job.getClass();
try {
Class printServiceClass = Class.forName("javax.print.PrintService"«»);
Method method = klass.getMethod("getPrintService", (Class[])null);
Object printService = method.invoke(job, (Object[])null);
method = klass.getMethod("setPrintService", new Class[]{printServiceClass});
method.invoke(job, new Object[] {printService});
} catch (Exception e) {

public static void main(String arg[]) {

Print2DPrinterJob sp = new Print2DPrinterJob(arg[0], arg[1]);

Post edited by: dchristm77, at: 2007/08/16 23:27

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...