CONTENTS | PREV | NEXT Java Print Service API User Guide


Submitting the Print Job

Once you have the Doc and DocPrintJob, you can call the DocPrintJob object's print method to submit the document to the service. The Submitting the Print Job to the Printer section completes the printin g example. The Submitting the Print Job to the Stream section completes the streaming example.


Submitting the Print Job to the Printer

This section completes the printing application explained in this chapter. This example prints five copies of a PostScript document, double-sided on A4 paper, and stapled.

    DocFlavor psFlavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new Copies(2));
    aset.add(MediaSizeName.ISO_A4);
    aset.add(Sides.TWO_SIDED_LONG_EDGE);
    aset.add(Finishings.STAPLE);
    PrintService[] pservices = PrintServiceLookup.lookupPrintServices(psFlavor,
                            aset);
    if (services.length > 0) {
        DocPrintJob pj = pservices[0].createPrintJob();
        try {
            FileInputStream fis = new FileInputStream("example.ps");
            Doc doc = new SimpleDoc(fis, psFlavor, null);
            pj.print(doc, aset);
        } catch (IOException e) {
            System.err.println(e);
        } catch (PrintException e) {
            System.err.println(e);
        }
    }
See Example: PrintPS.java for the complete application.


Submitting the Print Job to the Stream

This section completes the streaming example explained in this chapter. This example converts a GIF document to PostScript and embeds the specified printing attributes into the PostScript document.

    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new Copies(2));
    aset.add(MediaSizeName.ISO_A4);
    aset.add(Sides.TWO_SIDED_LONG_EDGE);
    aset.add(Finishings.STAPLE);
    StreamPrintServiceFactory[] factories =
        StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
                flavor, psMimeType);
    if(factories.length==0) {
        System.err.println("No suitable factories");
        System.exit(0);
    }
    try {
        FileInputStream fis = new FileInputStream("java2dlogo.gif");
        String filename = "newfile.ps";
        FileOutputStream fos = new FileOutputSteam(filename);
        StreamPrintService sps= factories[0].getPrintService(fos);
        DocPrintJob pj = sps.createPrintJob();
        Doc doc = new SimpleDoc(fos, psFlavor, aset);
        pj.print(doc, aset);
    } catch (IOException e) {
        System.err.println(e);
    } catch (PrintException e) {
        System.err.println(e);
    }
}
See Example: PrintGIFtoStream.java for the complete application.



CONTENTS | PREV | NEXT