开发者

Java - Can't print image (to paper/printer)

I am trying to print an Image using the following code, but the document simply stays in the print job queue, and refuses to print. In the (windows) print job queue I get:

DocumentName: Printing an image Status: [Nothing] Pages: 1, Size: 2.1Mb.

This does not happen with other applications using the same printer (word, etc).

Could anyone kindly show me where is my mistake? Thanks.

public static void main(String[] args) {
    //new Painter();

    MediaTracker tracker = new MediaTracker(new JPanel());

    try {
        Image img = ImageIO.read(new File(
            "C:\\Users\\David\\Desktop\\print.jpg"));
        tracker.addImage(img, 1);
        tracker.waitForAll();
        print(img);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

private static void print(final Image img) {
    PrinterJob printjob = PrinterJob.getPrinterJob();
    printjob.setJobName("Print");

    ImgPrinter printable = new ImgPrinter(img);

    try {
        System.out.println("Printing.");
        printable.printPage();
    } catch (PrinterException ex) {
        System.out.println("NO PAGE FOUND." + ex);
    }
}

private static class ImgPrinter implements Printable {

    Image img;

    public ImgPrinter(Image img) {
        this.img = img;
    }

    public int print(Graphics pg, PageFormat pf, int pageNum) {
        if (pageNum != 0) {
            return Printable.NO_SUCH_PAGE;
        }

        //BufferedImage bufferedImage = new BufferedImage(img.getWidth(null),
        //img.getHeight(null), BufferedImage.TYPE_INT_RGB);
        //bufferedImage.getGraphics().drawImage(img, 0, 0, null);

        Graphics2D g2 = (Graphics2D) pg;
        g2.translate(pf.getImageableX(), pf.getImageableY());
        g2.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
        return Printable.PAGE_EXISTS;
    }

    public void printPage() throws PrinterException {
        PrinterJob job = PrinterJob.开发者_运维知识库getPrinterJob();
        boolean ok = job.printDialog();
        if (ok) {
            job.setJobName("TEST JOB");
            job.setPrintable(this);
            job.print();
        }
    }
}

Screenshot of problem:

This happens with both hardware and software printers(XPS Writer, CutePDF, Canon printer). The hardware shows "preparing.." on it's screen forever, and I think it wasted all it's ink, I'm not sure. If so, this code has been expensive to test....

None of these printers give an issue when printing from a word document or otherwise.

Edit: Can somebody suggest a software printer he or she has been successful with?

Click here for the Image I am trying to print.

Click here to see the print queue.


I just ran a quick test, and it works fine for me. I was able to print a png image.

Chances are there is something wrong with your printer.

Did you try printing a Word Document using the Word's print option.

Are there multiple printers assigned to your machine? You could try restarting your printer? Restart you machine?

You could implement a print dialog box to open up. That way you can select the printer. See this link here. The code shows how to open up the print dialog in swing.

 public void printPage() throws PrinterException
        {
            PrinterJob job = PrinterJob.getPrinterJob();
            boolean ok = job.printDialog();
            if (ok) {
                job.setJobName("TEST JOB");
                job.setPrintable(this);
                job.print();
            }
        }

This way you can make sure a printer has been selected correctly.

The other thing you can use to make sure the image does not get distorted Instead of this line

g2.drawImage(bufferedImage, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);

use the following line in the inner class

g2.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜