Example usage for javax.print StreamPrintServiceFactory lookupStreamPrintServiceFactories

List of usage examples for javax.print StreamPrintServiceFactory lookupStreamPrintServiceFactories

Introduction

In this page you can find the example usage for javax.print StreamPrintServiceFactory lookupStreamPrintServiceFactories.

Prototype

public static StreamPrintServiceFactory[] lookupStreamPrintServiceFactories(DocFlavor flavor,
        String outputMimeType) 

Source Link

Document

Locates factories for print services that can be used with a print job to output a stream of data in the format specified by outputMimeType .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));

    OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));

    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
            DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

    if (factories.length > 0) {
        StreamPrintService service = factories[0].getPrintService(fos);
        DocPrintJob job = service.createPrintJob();
        Doc doc = new SimpleDoc(is, flavor, null);

        PrintJobWatcher pjDone = new PrintJobWatcher(job);

        job.print(doc, null);//  w ww .  j  a v  a  2s. c  om

        pjDone.waitForDone();
    }

    is.close();
    fos.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
            DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

    StreamPrintService service = factories[0].getPrintService(fos);
    final DocPrintJob job = service.createPrintJob();
    Doc doc = new SimpleDoc(is, flavor, null);

    PrintJobWatcher pjDone = new PrintJobWatcher(job);

    if (job instanceof CancelablePrintJob) {
        CancelablePrintJob cancelJob = (CancelablePrintJob) job;
        try {/*from   ww  w  .  j  a  va  2 s.  c o  m*/
            cancelJob.cancel();
        } catch (PrintException e) {
        }
    }
    job.print(doc, null);
    pjDone.waitForDone();
    is.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    try {// w w w  .j ava  2 s. c o  m
        OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
        DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
        InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));
        StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
                .lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

        StreamPrintService service = factories[0].getPrintService(fos);
        final DocPrintJob job = service.createPrintJob();
        Doc doc = new SimpleDoc(is, flavor, null);

        PrintJobWatcher pjDone = new PrintJobWatcher(job);

        if (job instanceof CancelablePrintJob) {
            CancelablePrintJob cancelJob = (CancelablePrintJob) job;
            try {
                cancelJob.cancel();
            } catch (PrintException e) {
            }
        }
        job.print(doc, null);
        pjDone.waitForDone();
        is.close();
    } catch (PrintException e) {
        if (e.getCause() instanceof PrinterAbortException) {
            System.out.println("Print job was cancelled");
        }
    }
}

From source file:JuliaSet3.java

public void save() throws IOException {
    // Find a factory object for printing Printable objects to PostScript.
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    String format = "application/postscript";
    StreamPrintServiceFactory factory = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
            format)[0];/* www .  j  av a2  s .c  o m*/

    // Ask the user to select a file and open the selected file
    JFileChooser chooser = new JFileChooser();
    if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION)
        return;
    File f = chooser.getSelectedFile();
    FileOutputStream out = new FileOutputStream(f);

    // Obtain a PrintService that prints to that file
    StreamPrintService service = factory.getPrintService(out);

    // Do the printing with the method below
    printToService(service, null);

    // And close the output file.
    out.close();
}

From source file:Print.java

public static void printToFile(String outputFileName, String outputFileType, String inputFileName,
        PrintRequestAttributeSet attributes) throws IOException {

    // Determine whether the system can print to the specified type, and
    // get a factory object if so.
    // The name of this static method is way too long!
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(null,
            outputFileType);//  ww w  .jav  a  2  s  .  c o m

    // Error message if we can't print to the specified output type
    if (factories.length == 0) {
        System.out.println("Unable to print files of type: " + outputFileType);
        return;
    }

    // Open the output file
    FileOutputStream out = new FileOutputStream(outputFileName);
    // Get a PrintService object to print to that file
    StreamPrintService service = factories[0].getPrintService(out);
    // Print using the method below
    printToService(service, inputFileName, attributes);
    // And remember to close the output file
    out.close();
}