Problem with SimpleDoc printing
Hi all,
I'm trying to print a simple text document with the below code but it keeps spitting out an illegal argument exception. That's after I've tweaked the DocFlavor to every other available type till I got one that shows the print dialog. Any help will be greatly appreciated as this is my first attempt at Java printing!
IllegalArgumentException:
java.lang.IllegalArgumentException: data is not of declared type
at javax.print.SimpleDoc.<init>(SimpleDoc.java:82)
at DiagnosticsPane.actionPerformed(DiagnosticsPane.java:350)
Code in question:
-
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
...
PrintRequestAttributeSet pras =new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintService ps[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200, ps, defaultService, flavor, pras);
if (service !=null){
try{
DocPrintJob job = service.createPrintJob();
DocAttributeSet das =new HashDocAttributeSet();
FileInputStream fis =new FileInputStream("report.txt");
Doc doc =new SimpleDoc(fis, flavor, das);
try{
job.print(doc, pras);
}catch (PrintException e){
CBTSys.errDialog("Print error!\n\n" + e.getMessage());
}
}catch (FileNotFoundException e){
CBTSys.errDialog("File not found!\n\n" + e.getMessage());
}
}else{
CBTSys.errDialog("No printer!\n\n");
}
[2564 byte] By [
marc79] at [2007-9-30 21:20:08]

IllegalArgumentException - if flavor or printData is null, or the printData does not correspond to the specified doc flavor--for example, the data is not of the type specified as the representation in the DocFlavor.
I found this from the javax.print API, but it doesn't make any sense to me. my DocFlavor (application/x-java-jvm-local-objectref; class="java.awt.print.Printable") and my plain text file doesn't match?
By the way, I've tried all the combinations of DocFlavor.*.* and SERVICE_FORMATTED.PRINTABLE and SERVICE_FORMATTED.PAGEABLE are the only ones working for me. Is this normal?
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
class testPrint
{
public static void main(String args[])
{
PrintRequestAttributeSet pras =new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintService ps[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200, ps, defaultService, flavor, pras);
if (service != null) {
try {
DocPrintJob job = service.createPrintJob();
DocAttributeSet das = new HashDocAttributeSet();
FileInputStream fis = new FileInputStream("report.txt");
Doc doc = new SimpleDoc(fis, flavor, das);
try {
job.print(doc, pras);
System.err.println("Job sent to printer.");
} catch (PrintException e) {
System.err.println("Print error!\n\n" + e.getMessage());
}
} catch (FileNotFoundException e) {
System.err.println("File not found!\n\n" + e.getMessage());
}
}
}
}