Printing in JDK 1.4,

xiaoxiao2021-03-06  103

Printing in JDK 1.4, Part 1

English original

content:

Locate a printer Specify Output Format Specify printing properties Settings content print printer dialog box A running example Reference About the author to this article

related information:

Design Patterns 101merlin's Magic Archive

There is also a Java area:

Teaching tools and product code and components all articles practical skills

JOHN Zukowski (Jaz@zukowski.net) President, JZ Ventures Company March 2002

After the version 1.1, it seems that each new release of the Java platform changes the platform's print support framework. Merlin is no exception. In this month

In Merlin's magic, John Zukowski starts discussing the latest features in two parts. Please click on the top or bottom of the article

Discussion

Discussions Forum share your experience in this article with author and other readers.

If you have already used a Java platform, you may be frustrated by me as an endless change of printing API. Merlin has brought another print function and technology for the Java platform. This latest re-version known as "Java Print Service API" has been applied to Java Community Process from 1999. Fortunately, the new API is a positive supplement and should represent the ultimate important modification of print support for a while. The process of printing using the new "Print Service API" includes three parts: discovery, specification, and printing. The fourth part is to be notified when the print task has progressed, which is optional. All classes and interfaces that will be used herein are in the Javax.Print package, or in one of its subcles (see Resources). The first step in positioning a printer executing the print job is to identify one or a set of printers you want to print. The printer object is called a print service, and the identification process is called a lookup. The class that supports the search task is just named PrintServiceLookup. To find print services, you need one of the three methods shown in Listing 1. Listing 1. Find a print service

Public Static Final PrintService

LOOKUPDEFAULTPRINTSERVICE ()

Public Static Final PrintService []

LookupprintServices (Docflavor Flavor, AttributeEt Attributes)

Public Static Final MultidocprintService []

LookUpmultidocprintServices (Docflavor [] Flavors,

AttributeSet Attributes)

Each of these three methods shown in Listing 1 is used for different tasks:

lookupDefaultprintService () Returns the default print service. LookupprintServices () Returns the printer set supporting a specific document type (such as GIF), and a specific properties set (such as double-sided print). lookupmultidocprintServices () supports a plurality of documents at a time. Once you want to use the print service you want, you need to create a print job. Later, you will send an output to this job. The PrintService returned by the lookup process can be created with its createprintjob () method, as shown below:

PrintService PrintService =

PrintServiceLookup.lookupdefaultprintService ();

DocprintJob Job = printService.createPrintJob (); Specify Output Format In addition to specifying where to print, you must specify the format of the print document. At this time, it is very convenient to use the DocFlavor class (or one of them). The DocFlavor class is used to identify the MIME (Multipurpose Internet Mail Extensions, multi-purpose Internet mail extensions) type you want to print. MIME Type Description How should I explain electronic data. When processing emails and accessories, you may have encountered MIME types, but the MIME specification describes more common mechanisms for identifying data formats. Merlin provides seven subclasses of Docflavor as itself is used to define internal classes in the format. These classes can be divided into three MIME type subsets: Bytes-oriented,-oriented and service-oriented. Bytestable style:

BYTE_ARRAY INPUT_STREAM URL is a character:

CHAR_ARRAY Reader String The service-oriented internal class is service_formatted. The internal class in the internal class supports its own MIME type set. These MIME types are also defined as internal classes. There are 19 kinds of bytes, as shown below:

AUTOSENSE GIF JPEG PCL PDF PNG POSTSCRIPT TEXT_HTML_HOST TEXT_HTML_US_ASCII TEXT_HTML_UTF_16 TEXT_HTML_UTF_16BE TEXT_HTML_UTF_16LE TEXT_HTML_UTF_8 TEXT_PLAIN_HOST TEXT_PLAIN_US_ASCII TEXT_PLAIN_UTF_16 TEXT_PLAIN_UTF_16BE TEXT_PLAIN_UTF_16LE TEXT_PLAIN_UTF_8 character-oriented stream rather monotonous, offers only two formats:

Text_html text_plain is a service-oriented stream containing three formats:

Pageable Printable Renderable_image You can configure the style like this (in this example, configure it as a PNG image):

Docflavor flavor = docflavor.input_stream.png;

Specifying Print Properties Print, you can specify some properties to describe how you want to print a document. The sample attribute includes a copy number, which is to print which page and document image type (such as lateral and vertical). To specify attributes, you will need to use one of the following two classes:

DOCATTRIBUTESET is a single document specified feature. PrintRequestAttributeSet Specifies the characteristics of a single print job. To specify attributes for printing, you need to create an instance of an appropriate property set (DOCATTRIBUTESETATATTRIBUTESET) and populate it with your expectations. For those properties that are not specified, it is also necessary to configure a reasonable default value. The Javax.Print.attribute package has approximately 70 different properties, each attribute is defined as a separate class. Each attribute uses one or more property sets. In addition to the two attributes set in this article, there are two attribute sets available for query information. Javax.print.attribute package provides a public space to find all types. Below we will see a print run, it uses PrintRequestAttributeSet and prints the object:

PrintRequestAttributeSet Pras = new hashprintrequestattributeset ();

Pras.Add (New Copies (5)); Be sure to see a list of all available properties in the javax.print.attribute package. Setting content DOC interface provides data for print jobs. The implementation of the interface is a SimpleDoc class. Using a constructor, the content is provided as the first parameter, the style as the second parameter, the attribute as the third parameter. This constructor is as follows:

Public SimpleDoc (Object PrintData, Docflavor Flavor,

DOCATTRIBUTESET Attributes)

This will leave a data issue for us. The answer depends on docflavor. If you specify the style of docflavor.input_stream, the data will be identified by its InputStream. If your style is docflavor.byte_Array, then the data is a byte array (byte []). So, you want to print a PNG image file, you need to use the syntax shown in Listing 2. Listing 2. Setting content

Docflavor flavor = docflavor.input_stream.png;

String filename = ...;

FileInputStream FII = New FileInputsteam (filename);

DOCATTRIBUTESET DAS = new hashdocattributeset ();

DOC DOC = New SimpleDoc (FIS, FLAVOR, DAS);

Print once you identify the printer, and specify the output format, attributes, and content, then just print it. The actual print job is performed by docprintJob (which is retrieved from PrintService), as shown in Listing 3. Listing 3. A print job

Docprintjob job = ...

PrintRequestAttributeSet PRAS = ...

DOC DOC = ...

Job.Print (DOC, PRAS);

By calling Print (), you triggered a mechanism, which sends content to the print service in a separate thread. The printer dialog is obvious, and we have not mentioned the print dialog so far. The dialog is popped up by the system, allowing you to configure printer properties by graphical mode. Figure 1 shows the print dialog of the new Java Print Service API. Figure 1. The printer dialog is interesting that the default behavior of the printer dialog has been changed with the new API: By default, the dialog is not displayed. So we must use the ServiceUI class to create the print dialog as shown above. The ServiceUI class provides a separate method to display the printer selection dialog:

PrintDialog (GraphicsConfiguration GC, INT X, INT Y,

PrintService [] Services, PrintService DefaultService,

Docflavor Flavor, PrintRequestAttributeSet Attributes)

Then you use the returned PrintService to get the docprintjob you want to use, as shown in Listing 4. Listing 4. Print using dialog

String filename = ...;

PrintRequestAttributeSet PRAS = ...

Docflavor flavor = ...

PrintService PrintService [] =

PrintServiceLookup.lookuppprintServices (Flavor, PRAS);

PrintService DefaultService = PrintServiceLookup.lookupdefaultprintService ();

PrintService Service = Serviceui.PrintDialog (NULL, 200, 200,

PrintService, DefaultService, Flavor, PRAS

IF (service! = null) {

DocprintJob Job = service.createprintjob ();

FileInputStream Fis = New FileInputStream (filename);

DOCATTRIBUTESET DAS = new hashdocattributeset ();

DOC DOC = New SimpleDoc (FIS, FLAVOR, DAS);

Job.Print (DOC, PRAS);

}

An operational example we will end this article with a runoffed example, which will allow you to test the new "Print Service API" function. The following code is essentially a running program that is combined by all previous code examples. When running the program, make sure the name of the PNG image file is entered on the command line. If you prefer to print a different format, just change the docflavor. Listing 5. A print example

Import javax.print. *;

Import.attribute. *;

Import java.io. *;

Public class printing {

Public static void main (string args []) throws exception {

String filename = args [0];

PrintRequestAttributeSet PRAS =

New hashprintrequestattributeset ();

Docflavor flavor = docflavor.input_stream.png;

PrintService PrintService [] =

PrintServiceLookup.lookuppprintServices (Flavor, PRAS);

PrintService DefaultService =

PrintServiceLookup.lookupdefaultprintService ();

PrintService Service = Serviceui.PrintDialog (NULL, 200, 200,

PrintService, DefaultService, Flavor, PRAS

IF (service! = null) {

DocprintJob Job = service.createprintjob ();

FileInputStream Fis = New FileInputStream (filename);

DOCATTRIBUTESET DAS = new hashdocattributeset ();

DOC DOC = New SimpleDoc (FIS, FLAVOR, DAS);

Job.Print (DOC, PRAS);

Thread.sleep (10000);

}

System.exit (0);

}

}

In the next part of Merlin's magic, you will learn how to print the screen or component. We will also discuss and print-related event handling tasks, and I will show you how to remove SLEEP () calls from the print operation. Reference

Click on the top of the article to participate in the discussion forum of this article at the top of the article. Learn how to use the complete "Java Print Service API". See the official documentation of Sun Microsystems. JSR-006 Unified Printing API briefly describes the origin of new print services. Relive the simple start of printing on the Java platform by "San Francisco Printing Primer With Java 1.1 / 1.2" (DeveloperWorks, Aprude 1999), it is possible to print on the Java platform (you can also understand information about IBM's SAN FRANCISCO project). Understand how the design pattern simplifies print applications. Please read the article "USING DESIGN PATTERNS to Simplify Printing in Java 1.1" (developerWorks, September 1997). If you know more about the design pattern, you can start learning from David Gallardo's "Design Patterns 101" tutorial, which is exclusive to developerWorks. Craig Mcelwee's "Personalizing PDF Files" provides a simple Perl-based approach to define PDF documents as a template and generating dynamic PDF (DeveloperWorks, February 2000). Reading John Zukowski about the review of the skills of using Merlin. In the Java technology area in DeveloperWorks, you can find hundreds of articles about Java programming. About the author John Zukowski is engaged in strategic Java advice through JZ Ventures, as well as resident guidelines for JGuru's Java-driven Java-driven Java. His nearest work has Apress published Java Collections and Definitive Guide to Swing For Java 2 (2nd Edition). You can contact John via jaz@zukowski.net.

Http://www-900.ibm.com/developerWorks/cn/java/j-merlin/part10/index.shtml

转载请注明原文地址:https://www.9cbs.com/read-104611.html

New Post(0)