2011-01-31 94 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打印和传真对话框,我可以从中打印。该程序调整图像大小,一切正常。