java - 使用通常的 Windows 打印对话框而不是 Java 的对话框

标签 java printing barcode printdialog

如何使用标准 Windows 打印对话框而不是 Java 对话框进行打印。我在使用标签打印机打印条形码时遇到问题。当我通过 Java 打印对话框打印时,我收到一条错误消息,提示我要打印的文档格式错误。当我打印到 XPS 文件然后通过 Windows 打印时,一切正常。希望有人能帮助我。

问候

最佳答案

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

关于java - 使用通常的 Windows 打印对话框而不是 Java 的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4854913/

相关文章:

java - 如何修复 Spring Boot 应用程序中 SimpleJdbcInsert 中的错误

java - 如何使用 javax.persistence.OneToMany 在 hibernate 4 中设置级联更新?

Java RMI - 从服务器上的远程方法返回对服务器上另一个远程对象的引用到客户端

java - 我需要关闭连接吗

excel - 将写入空格但不写入最后一个字符(VBA Excel)

android - 谷歌机器学习套件 : couldn't find "libbarhopper_v2.so"

javascript - 使用 JQuery 打开弹出窗口并打印

c# - 打印用户控件在顶部和左侧切掉半英寸

ios - UIWebview 在当前文本字段上设置文本

java - 如何提高 Graphics2D 文本质量?