0

I am able to print a .GIF, .JPG or .PNG successfully using the following code snippet but it doesn't work for .TIF file. Also I can't get the color even after adding the chromaticity.color attribute.

public class PrintImage {
    static public void main(String args[]) throws Exception {
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    pras.add(new Copies(1));
    pras.add(chromaticity.color);
    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();
    String fileName = "C:/labels/2.tif"
    FileInputStream fin = new FileInputStream(fileName);
    Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
    job.print(doc, pras);
    fin.close();
}

How do I support .TIF for printing?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1005747
  • 21
  • 1
  • 2
  • 7
  • *"able to print a .GIF, .JPG or .PNG"* Try `ImageIO.getReaderFileSuffixes()` to see **bmp**, gif, jpeg, jpg, png & **wbmp** listed in these times (on Windows, at least). That list will be longer if JAI is installed. See the [MediaTypes code](http://stackoverflow.com/questions/7585699/list-of-useful-environment-settings-in-java/7616206#7616206) for a more comprehensive listing of media support. – Andrew Thompson Oct 21 '11 at 00:06

1 Answers1

1

Use Java Advanced Imaging API for TIFF. JAI can handle multipage TIFF files, JPEG in TIFF and a few compression schemes. If you still have trouble printing, with the API you could convert your TIFF file to PNG.

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
  • thanks....i tried the JAI, but how do i get it to print using the above format...i.e. I added the following to the code RenderImage image=JAI.create("fileload",filename)....but i am not sure as how to print it using job.print(), and also will converting the tiff file to png degrade the quality of the image? – user1005747 Oct 21 '11 at 00:04
  • TIFF is a bit more complex than PNG or JPEG. One of the reasons for this is the fact that it can have many pages. It can hold lossless or lossy JPEG images as well as vector graphics for cropping. If your image is lossless it will remain lossless. Check this link for printing: https://forums.oracle.com/forums/thread.jspa?threadID=1287628 – Costis Aivalis Oct 21 '11 at 08:14
  • thanks ....i got the image printing. only i have some problem with the scaling. the above link was a great help. – user1005747 Oct 21 '11 at 14:54