• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help Printing  RSS feed

 
Matt Wilcko
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to print a jpeg that is stored as a BufferdImage. I do not want to have to save the file to the hard drive first so I thought I wound try BufferedImage. No luck, keep getting the error "data is not of the declared type". Im sure it has to do with the line when I pass the buffered image object to Doc: "Doc doc = new SimpleDoc(image, flavor, das);" The code is below. The code has it loading a file from the hard drive, when I integrate this into my program, I will simply pass the bufferedimage to it. Thanks!
import javax.print.*;
import javax.print.attribute.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.*;
public class Printing {
public static void main(String args[]) throws Exception {
String filename = "ad.jpg";
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
PrintService printService[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();

BufferedImage image = ImageIO.read(new FileInputStream(filename));
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(image, flavor, das);
job.print(doc, pras);
Thread.sleep(10000);
}
System.exit(0);
}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!