2011-01-31 97 views
3

如何使用標準Windows打印對話框來打印而不是Java對話框。使用標籤打印機打印條形碼時遇到問題。當我通過Java打印對話框打印時,出現一個錯誤,告訴我要打印的文檔格式不正確。當我打印到XPS文件,然後通過Windows打印它時,一切正常。希望任何人都可以幫助我。使用通常的Windows打印對話框而不是Java的對話框

問候

+0

你有一個文件可以打印嗎? – 2011-02-01 10:26:07

+0

你能張貼一些示例代碼嗎? – 2011-02-01 13:17:16

回答

0

這可能是從標籤打印機本身,而不是Java導致的誤差。嘗試用Java將數據寫入XPS文件,然後從Java打印。

+0

使用下面的代碼解決了它: – 2011-02-05 09:53:28

2
try { 
    Code128Bean bean = new Code128Bean(); 
    final int dpi = 150; 

    //Configure the barcode generator 
    bean.setModuleWidth(UnitConv.in2mm(2.0f/dpi)); //makes the narrow bar width exactly one pixel 
    bean.setBarHeight(10); 
    bean.doQuietZone(false); 

    //Open output file 
    File outputFile = new File("out.png"); 
    OutputStream out; 
    out = new FileOutputStream(outputFile); 

    //Set up the canvas provider for monochrome PNG output 
    BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 90); 

    // 200x 10 

    //Generate the barcode 
    bean.generateBarcode(canvas, barcode); 

    //Signal end of generation 
    canvas.finish(); 
    out.close(); 


    paintComponent(labelArtikelbezeichnung.getText()); 
    String working_dir = System.getProperty("user.dir"); 
    try { 
     Runtime rt = Runtime.getRuntime(); 
     String command = "C:\\WINDOWS\\system32\\rundll32.exe C:\\WINDOWS\\system32\\shimgvw.dll,ImageView_Fullscreen " + "" + working_dir + "\\out.png"; 
     System.out.println(command); 
     Process pr = rt.exec(command); 

     BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream())); 
     String line=null; 
     while((line=input.readLine()) != null) { 
      System.out.println(line); 
     } 

     int exitVal = pr.waitFor(); 
     System.out.println("Exited with error code "+exitVal); 

    } catch(Exception e) { 
     System.out.println(e.toString()); 
     e.printStackTrace(); 
    } 
} catch (IOException ex) { 
    System.out.println("Error creating the Barcode"); 
} 

該程序打開Windows打印和傳真對話框,我可以從中打印。該程序調整圖像大小,一切正常。