This discussion is archived
3 Replies Latest reply: Jun 20, 2012 2:42 AM by gimbal2 RSS

How to print Bufferred Image to a printer in java?

944659 Newbie
Currently Being Moderated
I have a bufferredImage object and I want to send it to a printer through my code. The following are the supported types for my printer:

image/gif; class="[B"

image/gif; class="java.io.InputStream"

image/gif; class="java.net.URL"

image/jpeg; class="[B"

image/jpeg; class="java.io.InputStream"

image/jpeg; class="java.net.URL"

image/png; class="[B"

image/png; class="java.io.InputStream"

image/png; class="java.net.URL"

application/x-java-jvm-local-objectref; class="java.awt.print.Pageable"

application/x-java-jvm-local-objectref; class="java.awt.print.Printable"

application/octet-stream; class="[B"

application/octet-stream; class="java.net.URL"

application/octet-stream; class="java.io.InputStream"

I have been successful by saving the buferredImage to a disk and then reopening it as fileInputStream and sending it to a print. But I want to do send the buferred image directly for printing.
I tried the following method:

DocFlavor dsc =
DocFlavor.INPUT_STREAM.PNG;

ByteArrayOutputStream os = new ByteArrayOutputStream();

image.flush();
try {
ImageIO.write(image, "png", os);
os.flush();

} catch (IOException e1) {
e1.printStackTrace();
}

InputStream is = new ByteArrayInputStream(os.toByteArray());


// create the doc to be sent to the printer
Doc doc =
new SimpleDoc(is, dsc, null);

and then sent the doc for printing but I am getting this exception - Unable to print document. Doc format not supported
So how I supposed proceed from now on.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points