I have to print a file (plain text) using javax.print api's. I am able to lookup the printer and submit a print job. But i am able to only print one copy of the file. Below is the code i have been using.
None of the options/attributes i specified using the PrintRequestAttributeSet is recognized by the printer. Though i specify 2 copies to print, the printer only prints one copy. Am i doing anything wrong here?
Printer used: Canon iR5050 PCL6
package com.print;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
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.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Finishings;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.NumberUp;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.Sides;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
public class TestPrint {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "D:/test.log";
// Open the file
InputStream in = null;
try {
in = new FileInputStream(fileName);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Figure out what type of file we're printing
DocFlavor myFormat = getFlavorFromFilename(fileName);
// Create a Doc
Doc myDoc = new SimpleDoc(in, myFormat, null);
// Build a set of attributes
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.LANDSCAPE);
aset.add(new Copies(2));
aset.add(Sides.DUPLEX);
aset.add(MediaSizeName.NA_LETTER);
aset.add(new NumberUp(2));
aset.add(Finishings.STAPLE);
// discover the printers that can print the format according to the
// instructions in the attribute set
PrintService[] services = PrintServiceLookup.lookupPrintServices(
myFormat, aset);
// Create a print job from one of the print services
if (services.length > 0) {
System.out.println("The print sent to>>>" + services[0].getName());
DocPrintJob job = services[0].createPrintJob();
// Monitor the print job with a listener
job.addPrintJobListener(new PrintJobAdapter() {
public void printDataTransferCompleted(PrintJobEvent e) {
System.out.println("Data transfer completed!");
}
public void printJobNoMoreEvents(PrintJobEvent e) {
System.out.println("No more events!");
}
public void printJobRequiresAttention(PrintJobEvent e) {
System.out.println("Requires Attention!");
}
public void printJobFailed(PrintJobEvent e) {
System.out.println("Print Job Failed!");
}
public void printJobCompleted(PrintJobEvent e) {
System.out.println("Print Job Completed!");
}
public void printJobCanceled(PrintJobEvent e) {
System.out.println("Print Job Cancelled!");
}
});
try {
job.print(myDoc, aset);
} catch (PrintException pe) {
pe.printStackTrace();
}
System.out.println("The print job ........");
}
}
// A utility method to return a DocFlavor object matching the
// extension of the filename.
public static DocFlavor getFlavorFromFilename(String filename) {
String extension = filename.substring(filename.lastIndexOf('.') + 1);
extension = extension.toLowerCase();
if (extension.equals("gif"))
return DocFlavor.INPUT_STREAM.GIF;
else if (extension.equals("jpeg"))
return DocFlavor.INPUT_STREAM.JPEG;
else if (extension.equals("jpg"))
return DocFlavor.INPUT_STREAM.JPEG;
else if (extension.equals("png"))
return DocFlavor.INPUT_STREAM.PNG;
else if (extension.equals("ps"))
return DocFlavor.INPUT_STREAM.POSTSCRIPT;
else if (extension.equals("txt"))
return DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST;
// Fallback: try to determine flavor from file content
else
return DocFlavor.INPUT_STREAM.AUTOSENSE;
}
}
After long research and numerous hours of testing, found the following inferences,
1) DocFlavor.INPUT_STREAM.AUTOSENSE would not support print attributes ○ http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4876267
2) DocFlavor.INPUT_STREAM.POSTSCRIPT would not support javax.print attributes either ○ Refer Bug ID 4722601 (Not able to post more than one link)
3) No windows API for JDK to find whether the print supports PCL ○ Refer Bug ID 4529660 (Not able to post more than one link)
So none of the above are defects as specified in the sun support forum. So the only option i found is to snap in all the POSTSCRIPT commands in to the Postscript file and use Javax.print api to print this document. Make sure to set the flavor as AUTOSENSE.
Also make sure your printer supports POSTSCRIPT format. You can also try using PJL. Hope this information helps!
Regards, Gokul
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With