2011-12-05 50 views
1

我有這樣的問題,即當您在Windows中並嘗試通過JAVA打印時,您只能使用AUTOSENSE屬性。 但是我想要打印的字符串是希臘語=> UTF-8。當我將AUTOSENSE轉到TEXT_PLAIN_UTF8時,我得到一個:sun.print.PrintJobFlavorException:無效的風味異常....Java Windows UTF-8(unicode)打印

有什麼建議嗎?或者用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) 

最後支持的口味有這些...

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" 
+0

我們不得不猜測,因爲你沒有包括完整的堆棧跟蹤。首先,你正在做UTF-8,但你的例子是使用'TEXT_PLAIN_UTF_16'。相反,你應該嘗試'... UTF_8'枚舉。另外,你是否在原始字符串上嘗試了'DocFlavor docFlavor = DocFlavor.STRING.TEXT_PLAIN;',繞過了整個明確的UTF-8字節數組轉換?如果錯誤仍然存​​在,那麼問題出在你*不*顯示我們。 – lavinio

+0

由於某些原因,我無法在Windows中使用字符串文本純文本選項。 –

回答

1

它更容易使用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(); 
      } 
     } 
    } 
} 
0

您應該使用DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_8

+0

也可以粘貼整個堆棧跟蹤 –

+0

是的,但是當我得到我的打印機支持的風味時,它只支持自動感應,儘管它不老。任何其他選項或其他庫我可以使用?謝謝! –

0

對於auto_sense,您可以在文本前寫入一個BOM字符(\ uFEFF)。這是一個零寬度空間,用於將文本標記爲unicode。

+0

感謝您的幫助,但它沒有工作.... 我用SWT做到了! –