Java Windows UTF-8(unicode)打印【JAVA教程】

!
也想出现在这里? 联系我们
信息

Java Windows UTF-8(unicode)打印,第1张

概述Java Windows UTF-8(unicode)打印

我有这样的问题,当你在windows中,你尝试打印通过JAVA只能使用autoSENSE属性。 但是我想要打印的string是希腊语=> UTF-8。 当我把autoSENSE到TEXT_PLAIN_UTF8我得到一个:sun.print.PrintJobFlavorException:无效的风味exception….

有什么build议么? 或者其他的Unicode打印方式? 谢谢!

String datastr = \”UNICODE STRING\”; byte[] databa = null; try { databa = datastr.getBytes(\”UTF8\”); } catch (UnsupportedEnCodingException e1) { e1.printstacktrace(); } DocFlavor docFlavor = DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_16; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); PrintService service = PrintServiceLookup.lookupDefaultPrintService(); if (databa != null) { DocPrintJob pjob = service.createPrintJob(); Doc doc = new SimpleDoc(databa,docFlavor,null); try { pjob.print(doc,aset); } catch (PrintException e) { e.printstacktrace(); }

如果我尝试打印它在STRING.TEXT_PLAIN和其他一切比autoSENSE,我得到这个:

sun.print.PrintJobFlavorException: invalID flavor at sun.print.Win32PrintJob.print(UnkNown Source)

最后支持的味道是这些…

打印时获取文件名称

如何以编程方式设置打印机属性?

非托pipeC ++中的默认打印机

如何将打印机驱动程序转换为独立的控制台应用程序,该应用程序可以生成包含要发送到打印机的字节的打印机文件?

添加裁剪标记到现有的pdf文件

Win32 Printer : HP Deskjet 5440 SerIEs Flavors: image/gif; image/gif; image/gif; image/jpeg; image/jpeg; image/jpeg; image/png; image/png; image/png; application/x-java-jvm-local-objectref; application/x-java-jvm-local-objectref; application/octet-stream; application/octet-stream; application/octet-stream;

是否有可能使用PHP打印文件

打印pdf文件并在打印完成后删除文件

在.NET中编写虚拟打印机

从windows窗体打印的工具

如何确定打印机是否支持双向通讯

它更容易与SWT在这里做代码…

package printer; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.printing.Printer; import org.eclipse.swt.printing.PrinterData; public class TextPrinter { Printer printer; GC gc; int lineHeight = 0; int tabWIDth = 0; int leftmargin; int rightmargin; int topmargin,bottommargin; int x; int y; int index; int end; String tabs; StringBuffer wordBuffer; public TextPrinter() { } public voID printString(final String texttoprint) { PrinterData data = Printer.getDefaultPrinterData(); printer = new Printer(data); Thread printingThread = new Thread(\”Printing\”) { @OverrIDe public voID run() { print(printer,texttoprint); printer.dispose(); } }; printingThread.start(); } voID print(Printer printer,String texttoprint) { if (printer.startJob(\”iasSAEl\”)) { Rectangle clIEntArea = printer.getClIEntArea(); Rectangle trim = printer.computeTrim(0,0); Point dpi = printer.getDPI(); leftmargin = dpi.x + trim.x; // one inch from left sIDe of paper rightmargin = clIEntArea.wIDth – dpi.x + trim.x + trim.wIDth; topmargin = dpi.y + trim.y; // one inch from top edge of paper bottommargin = clIEntArea.height – dpi.y + trim.y + trim.height; /* Create a buffer for computing tab wIDth. */ int tabSize = 4; // is tab wIDth a user setting in your UI? StringBuffer tabBuffer = new StringBuffer(tabSize); for (int i = 0; i < tabSize; i++) tabBuffer.append(\’ \’); tabs = tabBuffer.toString(); /* * Create printer GC,and create and set the printer Font & * foreground color. */ gc = new GC(printer); Font Font = new Font(null,\”Helvetica\”,11,SWT.norMAL); gc.setFont(Font); tabWIDth = gc.stringExtent(tabs).x; lineHeight = gc.getFontMetrics().getHeight(); /* Print text to current gc using word wrap */ printText(texttoprint); printer.endJob(); /* Cleanup graphics resources used in printing */ Font.dispose(); gc.dispose(); } } voID printText(String texttoprint) { printer.startPage(); wordBuffer = new StringBuffer(); x = leftmargin; y = topmargin; index = 0; end = texttoprint.length(); while (index < end) { char c = texttoprint.charat(index); index++; if (c != 0) { if (c == 0x0a || c == 0x0d) { if (c == 0x0d && index < end && texttoprint.charat(index) == 0x0a) { index++; // if this is cr-lf,skip the lf } printWordBuffer(); newline(); } else { if (c != \’t\’) { wordBuffer.append(c); } if (Character.isWhitespace(c)) { printWordBuffer(); if (c == \’t\’) { x += tabWIDth; } } } } } if (y + lineHeight <= bottommargin) { printer.endPage(); } } voID printWordBuffer() { if (wordBuffer.length() > 0) { String word = wordBuffer.toString(); int worDWIDth = gc.stringExtent(word).x; if (x + worDWIDth > rightmargin) { /* word doesn\’t fit on current line,so wrap */ newline(); } gc.drawString(word,x,y,false); x += worDWIDth; wordBuffer = new StringBuffer(); } } voID newline() { x = leftmargin; y += lineHeight; if (y + lineHeight > bottommargin) { printer.endPage(); if (index + 1 < end) { y = topmargin; printer.startPage(); } } } }

您应该使用DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_8 。

对于auto_sense,你可以在文本前写一个BOM字符( uFEFF )。 这是一个零宽度空间,用于将文本标记为unicode。

总结

以上是内存溢出为你收集整理的Java Windows UTF-8(unicode)打印全部内容,希望文章能够帮你解决Java Windows UTF-8(unicode)打印所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

© 版权声明
THE END
喜欢就支持一下吧
点赞128 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容