Java Windows UTF-8 (unicode) 打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8390877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Windows UTF-8 (unicode) Printing
提问by Yannis Assael
I have the problem that when you are in windows and you try to print through JAVA you can only use the AUTOSENSE property. However my string that I want to get printed is in greek => UTF-8. When I turn the AUTOSENSE to TEXT_PLAIN_UTF8 i get an: sun.print.PrintJobFlavorException: invalid flavor exception....
我有一个问题,当您在 Windows 中并尝试通过 JAVA 打印时,您只能使用 AUTOSENSE 属性。但是,我想打印的字符串是希腊语 => UTF-8。当我将 AUTOSENSE 转为 TEXT_PLAIN_UTF8 时,我得到一个: sun.print.PrintJobFlavorException: invalid flavor exception....
Any suggestions? Or other way of printing in Unicode? thanks!
有什么建议?或其他以 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();
}
if i try to print it in STRING.TEXT_PLAIN and also in everything else than AUTOSENSE, i get this:
如果我尝试在 STRING.TEXT_PLAIN 以及除 AUTOSENSE 之外的所有其他内容中打印它,我会得到以下信息:
sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Unknown Source)
Finally the supported Flavors are these...
最后支持的 Flavors 是这些......
Win32 Printer : HP Deskjet 5440 Series Flavors:
image/gif; class="[B"
image/gif; class="java.io.InputStream"
image/gif; class="java.net.URL"
image/jpeg; class="[B"
image/jpeg; class="java.io.InputStream"
image/jpeg; class="java.net.URL"
image/png; class="[B"
image/png; class="java.io.InputStream"
image/png; class="java.net.URL"
application/x-java-jvm-local-objectref; class="java.awt.print.Pageable"
application/x-java-jvm-local-objectref; class="java.awt.print.Printable"
application/octet-stream; class="[B"
application/octet-stream; class="java.net.URL"
application/octet-stream; class="java.io.InputStream"
采纳答案by Yannis Assael
Its easier to do it with SWT here is the code...
使用 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, 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();
}
}
}
}
回答by Amir Raminfar
You should use DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_8
.
你应该使用DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_8
.
回答by Joop Eggen
For auto_sense you could write a BOM character (\uFEFF) in front of the text. This is a zero-width space, used for marking a text as unicode.
对于 auto_sense,您可以在文本前写一个 BOM 字符 ( \uFEFF)。这是一个零宽度空间,用于将文本标记为 unicode。