Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help Printing

Matt Wilcko
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.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 =
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();

BufferedImage image = FileInputStream(filename));
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(image, flavor, das);
job.print(doc, pras);
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic