【问题标题】:Graphics2D drawImage won't send correctly to printerGraphics2D drawImage 无法正确发送到打印机
【发布时间】:2017-01-12 21:20:15
【问题描述】:

我的问题是,我正在尝试使用 Java 进行打印,而且每次似乎都会给出随机结果(看看图片你就会明白)。我第一次运行它时,图像打印得很好,但第二次有一个黑框覆盖了一半的屏幕。这是First Run

还有Second run

代码如下:

package test;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;

import javax.swing.*;
import java.awt.print.*;
import java.net.URL;

public class HelloWorldPrinter implements Printable, ActionListener {
    private Image ix = null;

    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

        if (page > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }

        /*
         * User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid clipping
         */
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        ix = getImage("Capture.JPG");
        g.drawImage(ix, 1, 1, null);

        return PAGE_EXISTS;
    }

    public void actionPerformed(ActionEvent e) {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        boolean ok = job.printDialog();
        if (ok) {
            try {
                job.print();
            } catch (PrinterException ex) {
                /* The job did not successfully complete */
            }
        }
    }

    public static void main(String args[]) {
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        JFrame f = new JFrame("Hello World Printer");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        JButton printButton = new JButton("Print Hello World");
        printButton.addActionListener(new HelloWorldPrinter());
        f.add("Center", printButton);
        f.pack();
        f.setVisible(true);
    }

    public Image getImage(String path) {
        Image tempImage = null;
        try {
            URL imageURL = HelloWorldPrinter.class.getResource(path);
            imageURL = HelloWorldPrinter.class.getResource(path);
            tempImage  = Toolkit.getDefaultToolkit().getImage(imageURL);
        } catch (Exception e) {
            System.out.println(e);
        }
        return tempImage;
    }
}

感谢您抽出宝贵时间阅读本文,希望您有解决方案。

编辑:我正在使用 Microsoft Print To PDF,以便查看打印。我不知道它是否相关,但无论如何我都会添加它。

【问题讨论】:

  • 不要使用Toolkit#getImage,这可能会使用线程加载图像或以意想不到的方式缓存结果,请考虑使用ImageIO.read,它会阻塞直到图像完全实现.也有可能是你的getImage方法触发了异常,返回的是空白图片,但是由于你忽略了异常结果,所以很难知道
  • 感谢@MadProgrammer 使用ImageIO.read 工作。

标签: java printing graphics2d


【解决方案1】:

MadProgrammer 的解决方案奏效了。

不要使用 Toolkit#getImage,这可能会使用线程加载图像或以意想不到的方式缓存结果,请考虑使用 ImageIO.read 代替,它会阻塞直到图像完全实现。也有可能是你的getImage方法触发了异常,返回的是空白图片,但是由于你忽略了异常结果,所以很难知道——MadProgrammer

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多