• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing Error while printing txt file

 
Rodricks george
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi when i run the below code i am getting Exceptions... like flavour doesnt supp... pls help

import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

import java.awt.GraphicsConfiguration;
//import java.awt.print.PrinterJob;

public class BasicPrint {
public static void main(String[] args) {
try {
// Open the image file
//InputStream is = new BufferedInputStream(
//new FileInputStream("E:\\websites.txt"));
FileInputStream is = new FileInputStream("E:\\websites.txt");
// Find the default service
//DocFlavor flavor = DocFlavor.READER.TEXT_HTML;
//DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_16BE;
//DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST;
// PrintService service =
// PrintServiceLookup.lookupDefaultPrintService();
PrintService dservice = PrintServiceLookup.lookupDefaultPrintService();

PrintService[] services = PrintServiceLookup.lookupPrintServices(
flavor, null);

if (services == null || services.length < 1)
services = PrintServiceLookup.lookupPrintServices(null, null);

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
aset.add(OrientationRequested.PORTRAIT);
// aset.add(MediaTray.MAIN);
aset.add(Sides.ONE_SIDED);
aset.add(MediaSizeName.ISO_A4);

PrintService service = ServiceUI.printDialog(
(GraphicsConfiguration) null, 60, 60, services,
(PrintService) dservice, (DocFlavor) flavor, aset);

if (service != null) {

// Create the print job
final DocPrintJob job = service.createPrintJob();

Doc doc = new SimpleDoc(is, flavor, null);
try {

// Print it

job.print(doc, (PrintRequestAttributeSet) aset);

} catch (PrintException e) {
e.printStackTrace();
}

// Monitor print job events; for the implementation of
// PrintJobWatcher,

//PrintJobWatcher pjDone = new PrintJobWatcher(job);

// Wait for the print job to be done
//pjDone.waitForDone();

// It is now safe to close the input stream
is.close();
}
}catch (IOException e) {
}


}
}
------------------------------------------------------------------------

Exception in thread "main" java.lang.IllegalArgumentException: text/plain; charset="cp1252"; class="java.io.InputStream" is an unsupported flavor
at sun.print.Win32PrintService.getSupportedAttributeValues(Unknown Source)
at sun.print.ServiceDialog$MediaPanel.updateInfo(Unknown Source)
at sun.print.ServiceDialog$PageSetupPanel.updateInfo(Unknown Source)
at sun.print.ServiceDialog.updatePanels(Unknown Source)
at sun.print.ServiceDialog.initPrintDialog(Unknown Source)
at sun.print.ServiceDialog.<init>(Unknown Source)
at javax.print.ServiceUI.printDialog(Unknown Source)
at BasicPrint.main(BasicPrint.java:40)
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I understand Java printing, the DocFlavor isn't used to tell Java what the document is, it is used by Java to tell the printer what the document is. If your printer doesn't support directly printing that kind of document, you receive "unsupported flavor" exceptions.
You can discover the DocFlavors a particular printer can handle with PrintService.getSupportedDocFlavors().
This is probably the same problem you are having with printing PDFs.
I've had the best luck with the AUTOSENSE DocFlavors.
 
Rodricks george
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you sir
-Rodricks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic