I need to print on a printer an image and some data, in java (Swing) but all in vain. I am just able to do up with the data but not with the image. I have a abc.png file and 6 jTextBoxes, from which the value has to be printed on the printer. I am using the FileWriter and PrinterJob classes to implement the job. The data and the image can be printed separately but not together. Can somebody suggest me something.
Thanks.
Code for printing image:
try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
if (pss.length == 0)
throw new RuntimeException("No printer services available.");
PrintService ps = pss[0];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
FileInputStream fin = new FileInputStream("C://a.gif");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
//Doc doc1=new SimpleDoc();
job.print(doc, pras);
//fin.close();
} catch (IOException ie) {
ie.printStackTrace();
} catch (PrintException pe) {
pe.printStackTrace();
}
Code for printing text:
Just pass the location of the file on the hard disk, in FileInputStream.
One solution could be to create a second image that contains your original image and the text.
Something like this may work? (un-tested):