This discussion is archived
2 Replies Latest reply: Apr 17, 2011 10:04 PM by 855532 RSS

Problem in printing Chinese characters by using JavaPrintService

855532 Newbie
Currently Being Moderated
Hi, I just develop a tool to print txt files by using Java Print Service.

Everything seems to be good, but when the file contains Chinese character, it can't be print correctly.

I tried to use the DocFlavor.Reader and DocFlavor.STRING, however all of them are failed.

My code is below, please help me, thanks so much

PrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();
// TODO:Printing is not correct because of the Chinese Encoding
DocFlavor flavor = new DocFlavor.INPUT_STREAM("text/plain;charset=UTF-8");//DocFlavor.INPUT_STREAM.AUTOSENSE;

PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
if(defaultService != null){
Iterator<String> it = filename.iterator();
while(it.hasNext()){
shortname = it.next().toString();
abstractPath = GlobalVar.SRC_DIRECTORY_PATH + shortname;
File file = new File(abstractPath);
if(file.exists()){
try{
DocPrintJob job = defaultService.createPrintJob();
FileInputStream fis = new FileInputStream(file);
//BufferedReader br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis,flavor,das);
job.print(doc, pars);
}catch(PrintException ex){
Log.doLog("Print failed\t" + shortname, Log.ERROR);
System.out.println(ex.fillInStackTrace());
}
}else{
Log.doLog("File can't be found: "+abstractPath, Log.ERROR);
}
}
}else{
Log.doLog("Can't find printer", Log.ERROR);
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points