Using the usual Windows Print Dialog instead of Java's dialog

2.1k views Asked by At

How can I use the standard Windows Print dialog to print instead of the Java Dialog. I got problems printing a barcode with a label-printer. When I print via the Java-Print Dialog I get an error telling me that the format of the document which I want to print is wrong. When I print to a XPS File and then print it via Windows, everything works perfectly. Hope anyone can help me.

regards

2

There are 2 answers

1
Kevin Goedecke On
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");
}

The program opens the Windows Print and Fax Dialog where I can print from. The program resizes the image and everything works perfectly.

0
Casey On

It's probably an error resulting from the label printer itself, not Java. Try writing your data to an XPS file with Java and then printing that from Java.