CONTENTS | PREV | NEXT
To create a Doc you
must provide an implementation of the Doc interface. The Java Print
Service API provides a convenient implementation of Doc called
SimpleDoc. An application is not required to use the SimpleDoc
implementation, but to ensure compliance with Doc, any Doc
implementation must observe the same required semantics that
SimpleDoc implements, which are:
- Every Doc
implemenation must implement all five methods of the Doc
interface.
- The Doc
implementation must allow multiple threads to access the Doc
object.
- Every time a Doc
method is called, it returns the same object. This means you do not
return a new stream. Since there is only one input stream there can
only be one consumer of the Doc.
- The Doc returns a
stream for the service if requested
- The Doc checks if
the data type matches the doc flavor
- The attributes
returned from getAttributes always override those passed in the
print method.
Before creating a Doc,
you need to load your document from the file. The representation
class of the DocFlavor determines how you load the document from
the file. In this case, the representation class is an InputStream:
FileInputStream fis = new FileInputStream("java2dlogo.gif");
Once you have the
stream, pass it to SimpleDoc with the DocFlavor and a
DocAttributeSet, ifi you have one. If you don't have a
DocAttributeSet, pass in null instead:
Doc doc = new SimpleDoc(fis, flavor, null);
See Example: PrintGIF.java for an example of a custom Doc
implementation.
The next section
demonstrates how to register for events on your print job or
service.
CONTENTS | PREV | NEXT