6

How can we print a buffered image in java? We can send FileInputStream to Print Service, but i need to send buffered Image to it.

FileInputStream fin = new FileInputStream("YOurImageFileName.PNG");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
job.print(doc, pras);

Is it possible?

Check the complete code here.

vivek
  • 4,599
  • 3
  • 25
  • 37
vicky
  • 1,046
  • 2
  • 12
  • 25
  • 1
    A similar question was already asked in StackOverflow, check [this question](http://stackoverflow.com/questions/5338423/print-a-image-with-actual-size-in-java) – AurA May 07 '12 at 09:28

2 Answers2

7
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(new Printable() {
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
                if (pageIndex != 0) {
                    return NO_SUCH_PAGE;
                }
                graphics.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
                return PAGE_EXISTS;
        }
});     
try {
    printJob.print();
} catch (PrinterException e1) {             
    e1.printStackTrace();
}
vivek
  • 4,599
  • 3
  • 25
  • 37
1

You can use the iText library.. there is a simple example to print an Image to pdf .

Adding an IText Image to a PDF document

UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
LyB8899
  • 313
  • 4
  • 14