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:
Doc
implementation must implement all five methods of the Doc
interface.Doc
implementation must allow multiple threads to access the Doc
object.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
.Doc
returns a
stream for the service if requestedDoc
checks if
the data type matches the DocFlavor
getAttributes
always override those passed in the
print
method.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
, if 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.