print

Discover Streaming Print Services

With this example we shall show you how to discover streaming print services in a Java Desktop Application. You will find this particularly useful when you want to create and handle print jobs from you application.

Discovering streaming print services is very easy as it requires that you :

  • Use StreamPrintServiceFactory.lookupStreamPrintServiceFactories(null, null) to locates factories for print services that can be used with a print.
  • Use StreamPrintServiceFactory.lookupStreamPrintServiceFactories to find all streaming factories that support particular input

Let’s see the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import javax.print.DocFlavor;
import javax.print.StreamPrintServiceFactory;

public class DiscoverStreamingPrintServices {

	public static void main(String[] args) {

		// locates factories for print services that can be used with a print
		// job to output a stream of data in the format specified by flavor
		// with null no constraints are used
		StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
				.lookupStreamPrintServiceFactories(null, null);

		System.out.println("Streaming Printer Factories found:");
		printFactories(factories);

		// find all streaming factories that support particular input
		// formats (e.g. JPEG and Postscript)
		factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
		    DocFlavor.INPUT_STREAM.GIF,
		    DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

		System.out.println("Streaming Printer Factories found (with JPEG/Postscript):");
		printFactories(factories);

	}

	private static void printFactories(StreamPrintServiceFactory[] factories) {
		if (factories!=null && factories.length>0) {
			for (int i = 0; i < factories.length; i++) {
				System.out.println("t" + factories[i].getClass().getCanonicalName());
			}
		}
	}

}

Output:

Streaming Printer Factories found:
	sun.print.PSStreamPrinterFactory
Streaming Printer Factories found (with JPEG/Postscript):
	sun.print.PSStreamPrinterFactory

 
This was an example on discovering Streaming Print Services.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button