Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6619885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:01:46+00:00 2026-05-25T21:01:46+00:00

I am trying to print an Image using the following code, but the document

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:01:46+00:00Added an answer on May 25, 2026 at 9:01 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to draw the following image: Using this objective-c code: CGContextSetFillColorWithColor(ctx, [[UIColor redColor]
I am trying to print an image file on printer using QWebview but instead
I am trying to print an image (from file) to a the printer using
I'm trying to print the output of function only when it is true but
I am trying to print a report that contains a bar graph using the
I am trying to print invoice. But top whatever i do to setting margins,
I'm trying to create imagemaps on an image in wpf using codebehind. See the
I am trying to POST an image to imageshack using their API and Play
I'm trying to load fragments of XHTML markup using jQuery's $.fn.load function, but it
I'm using the following code to resize a tif. The tif has an alpha

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.