Computing.Net > Forums > Programming > Please help JAVA PRINTING API 1.4

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Please help JAVA PRINTING API 1.4

Reply to Message Icon

Name: Allan
Date: March 25, 2002 at 19:54:52 Pacific
Comment:

THIS IS THE CODE WHICH SUN has given on their site for GIF printing.
This code works fine on my machine for
JPG document also.
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
/*
* Use the Java(TM) Print Service API to locate a print service which
* can print a GIF-encoded image. A GIF image is printed according to
* a job template specified as a set of printing attributes.
*/
public class PrintGIF {

public static void main(String args[]) {

/* Use the pre-defined flavor for a GIF from an InputStream */
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;

/* Create a set which specifies how the job is to be printed */
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.NA_LETTER);
aset.add(new Copies(1));

/* Locate print services which can print a GIF in the manner specified */
PrintService[] pservices =
PrintServiceLookup.lookupPrintServices(flavor, aset);

if (pservices.length > 0) {
/* Create a Print Job */
DocPrintJob printJob = pservices[0].createPrintJob();

/* Create a Doc implementation to pass the print data */
Doc doc = new InputStreamDoc("java2dlogo.gif", flavor);

/* Print the doc as specified */
try {
printJob.print(doc, aset);
} catch (PrintException e) {
System.err.println(e);
}
} else {
System.err.println("No suitable printers");
}
}
}

class InputStreamDoc implements Doc {
private String filename;
private DocFlavor docFlavor;
private InputStream stream;

public InputStreamDoc(String name, DocFlavor flavor) {
filename = name;
docFlavor = flavor;
}

public DocFlavor getDocFlavor() {
return docFlavor;
}

/* No attributes attached to this Doc - mainly useful for MultiDoc */
public DocAttributeSet getAttributes() {
return null;
}

/* Since the data is to be supplied as an InputStream delegate to
* getStreamForBytes().
*/
public Object getPrintData() throws IOException {
return getStreamForBytes();
}

/* Not possible to return a GIF as text */
public Reader getReaderForText()
throws UnsupportedEncodingException, IOException {
return null;
}

/* Return the print data as an InputStream.
* Always return the same instance.
*/
public InputStream getStreamForBytes() throws IOException {
synchronized(this) {
if (stream == null) {
stream = new FileInputStream(filename);
}
return stream;
}
}
}

but as soon as i change the document type to
application/pdf, it doesn't work.Please DO reply.Thanks in advance.



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Please help JAVA PRINTING API 1.4

JAVA PRINTING API www.computing.net/answers/programming/java-printing-api/1111.html

JAVA PRINTING API 1.4 www.computing.net/answers/programming/java-printing-api-14/1134.html

Please help with java program. www.computing.net/answers/programming/please-help-with-java-program/14834.html