Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java PrintService printing Chinese/Japanese characters output is garbled.

67764Jan 12 2011
Hi Java Gurus,

I have been spending the whole day trying to make our text printing application able to print non-western Characters (e.g. Chinese and Japanese) as part of our requirements. But I am constantly getting garbled output when I print a UTF-8 formatted text file containing Chinese characters. I've been trying to switch the DocFlavor types (e.g. byte array in UTF 8, input stream auto sense, UT8-8, etc) but I couldn't simply make it work.

Here is our test method:

public void testPrintText(String fileName) throws Exception {
FileInputStream textStream = new FileInputStream(fileName);
File file = new File(fileName);
String fileContent = FileUtils.readFileToString(file);
DocFlavor flavor = DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_8;
//DocFlavor flavor = DocFlavor.READER.TEXT_PLAIN;
InputStreamReader isr = new InputStreamReader(textStream, "utf-8");
DocAttributeSet das = new HashDocAttributeSet();
//System.out.println("host encoding: " + flavor.hostEncoding);
Doc mydoc = new SimpleDoc(fileContent.getBytes("utf-8"), flavor, das);
//Doc mydoc = new SimpleDoc(textStream, flavor, das);
PrintRequestAttributeSet pas = new HashPrintRequestAttributeSet();
pas.add(new PrinterName("\\\\fsinec\\Canon iR5055 PCL6", null));
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, pas);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
System.out.println("DEBUG: " + defaultService.getClass().getName());
if(services.length == 0) {
if(defaultService == null) {
//no printer found
} else {
//print using default
DocPrintJob job = defaultService.createPrintJob();
job.print(mydoc, pas);
}
} else {
//built in UI for printing you may not use this
PrintRequestAttributeSet attSet = new HashPrintRequestAttributeSet();
for(int ctr=0; ctr<services.length; ctr++) {
PrintService printService = services[ctr];
System.out.println("COTS DEBUG: " + defaultService.getClass().getName());
}

//attSet.add();
PrintService service = null;
if(services.length == 1) {
//assume that there is no other printer with the name \\\\fsinec\\Canon iR5055 PCL6
//resulting to fetch the printer services to only one (1) element.
service = services[0];
}

/* open a dialog box
* PrintService service =
ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, attSet);*/
if (service != null) {
DocPrintJob job = service.createPrintJob();
job.print(mydoc, attSet);
}
}
}


Please help me on this.

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 9 2011
Added on Jan 12 2011
0 comments
871 views