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.
Can We Display An Image In Java? JLabel can be used to display an image in Java. The file path is passed to the Files class by pass the Image’s path. ImageIO is next used to transform the image into a BufferedImage object. It is now time to create the icon for the JLabel.
The print () method is used to print text on the console. It is an overloaded method of the PrintStream class. It accepts a string as a parameter. After printing the statement, the cursor remains on the same line.
In Java, we usually use the println () method to print the statement. It belongs to the PrintStream class. The class also provides the other methods for the same purpose. In this section, we will learn how to print in Java. Along with this, we will also explain the statement System.out.println ().
JLabel extends JComponent, and we can attach this component to a JFrame. To read the image file, we use the File class and pass the path of the image. Next we convert the image to a BufferedImage object using ImageIO.read (). Now we create an icon to be shown in the JLabel.
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();
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With