Jump to content

Double Sided printing from Solaris platform


dchristm77

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?

 

Code:
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"«»)) {
aset.add(Sides.DUPLEX);
printStyleLabel = Sides.DUPLEX.toString();
} else if (printStyle.equalsIgnoreCase("Tumble"«»)) {
aset.add(Sides.TUMBLE);
printStyleLabel = Sides.TUMBLE.toString();
} else if (printStyle.equalsIgnoreCase("twoSidedShortEdge"«»)) {
aset.add(Sides.TWO_SIDED_SHORT_EDGE);
printStyleLabel = Sides.TWO_SIDED_SHORT_EDGE.toString();
} else if (printStyle.equalsIgnoreCase("twoSidedLongEdge"«»)) {
aset.add(Sides.TWO_SIDED_LONG_EDGE);
printStyleLabel = Sides.TWO_SIDED_LONG_EDGE.toString();
} else {
aset.add(Sides.ONE_SIDED);
printStyleLabel = Sides.ONE_SIDED.toString();
}

aset.add(OrientationRequested.LANDSCAPE);
aset.add(MediaSizeName.NA_LETTER);
aset.add(new Copies(1));
aset.add(new JobName("Jasper Test job", null));
aset.add(MultipleDocumentHandling.SINGLE_DOCUMENT);

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

/* Create a print job */
PrinterJob pj = PrinterJob.getPrinterJob();
initPrinterJobFields(pj);
pj.setPrintable(this);
/* 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 {
pj.setPrintService(services[0]);
pj.print(aset);
} catch (PrinterException pe) {
System.err.println(pe);
}
}
}

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

if (pageIndex < 2) {
Graphics2D g2d= (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.setColor(Color.GRAY);
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) {
e.printStackTrace();
}
}


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...