How to print image in java

How can we print a buffered image in java? We can send a FileInputStream to the print service, but I need to send a 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 out the full code here .

+4
source share
2 answers
 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(); } 
+5
source

You can use the iText library .. there is a simple example for printing an image in pdf.

Adding an IText Image to a PDF Document

+1
source

Source: https://habr.com/ru/post/1411071/


All Articles