2011-01-31 2 views
3

표준 Windows 인쇄 대화 상자를 사용하여 Java 대화 상자 대신 인쇄하는 방법은 무엇입니까? 라벨 프린터로 바코드를 인쇄하는 데 문제가 있습니다. Java-Print Dialog를 통해 인쇄 할 때 인쇄하고자하는 문서의 형식이 잘못되었다는 오류가 발생합니다. XPS 파일로 인쇄 한 다음 Windows를 통해 인쇄하면 모든 것이 완벽하게 작동합니다. 누군가가 나를 도울 수 있기를 바랍니다.Java 대화 상자 대신 일반적인 Windows 인쇄 대화 상자 사용

안부

+0

인쇄 할 수있는 파일이 있습니까? –

+0

몇 가지 샘플 코드를 게시 할 수 있습니까? –

답변

0

아마도 자바가 아닌 라벨 프린터 자체 때문에 발생하는 오류 일 수 있습니다. Java로 XPS 파일에 데이터를 작성한 다음 Java에서 인쇄하십시오.

+0

은 다음 코드를 사용하여 해결했습니다. –

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 인쇄 및 팩스 대화 상자가 열립니다. 프로그램이 이미지 크기를 조정하면 모든 것이 완벽하게 작동합니다.

+0

+1 우리 모두의 깨달음에 대한 자신의 질문에 대답합니다. – Jan