Java Printing Program Design Complete Introduction

Source: Internet
Author: User
Tags print object

Objective

In our actual work, we often need to implement the printing function. But for historical reasons, Java provides a weaker print function. In fact, the original JDK did not support printing at all, until jdk1.1 introduced a very lightweight print support. Therefore, in the previous design with Java/applet/jsp/servlet program, the more complex printing is by calling Activex/ocx control or VB/VC program to achieve, very troublesome. In fact, Sun has been working to improve the functionality of Java printing, and the JAVA2 platform finally has a robust printing mode of the beginning, the printing mode and JAVA2D graphics package fully integrated into one. More encouragingly, the newly released jdk1.4 provides a complete set of "Java Print Service APIs" (the Java Printer Services API), which is a positive complement to existing printing capabilities. With it, we can achieve most of the actual application requirements, including print text, graphics, files and print preview, and so on. This article will show you how to design Java printing program to implement these functions through a specific program example, and compare the implementation methods of different versions, and hope that we can get some useful hints from them.

Printing in Java

1, the Java Printing API

The Java Print API mainly exists in the Java.awt.print package. The new classes in jdk1.4 are mainly found in javax.print packages and their corresponding child packages javax.print.event and Javax.print.attribute. The Javax.print package contains the related classes for the print service, while javax.print.event contains definitions of the print events, Javax.print.attribute includes a list of available attributes for the Print service.

2, how to achieve printing

To produce a print, you need to consider at least two articles:

A Print service object is required. This can be done in three ways: Before the jdk1.4 version, you must implement the Java.awt.print.Printable interface or pass Toolkit.getdefaulttoolkit (). Getprintjob to get Print service objects In jdk1.4, you can find the location of a print service object by Javax.print.PrintSerivceLookup.

You need to start a print job. There are several ways to do this: Before jdk1.4, you can start printing by calling the print or Printall method by Java.awt.print.PrintJob (jdk1.1 provided, now rarely used) Or you can display the Print dialog box through the Java.awt.print.PrinterJob PrintDialog, and then start printing by using the Print method, and in jdk1.4 you can display the Print dialog box through Javax.print.ServiceUI PrintDialog. Then call the Print method to start a print job.

3. Printer dialog box

3.1 Printable Print dialog box

Before you start printing, you can display a print dialog box by Printerjob.printdialog. It gives the user an opportunity to select the range of pages that should be printed and to change the print settings for the user. It is a local dialog box.

In fact, when you do a print job from a printable object, the Print object doesn't know how many pages you need to print. It just keeps calling the Print method. As long as the Print method returns the Printable.page_exists value, the printing work does not stop until the Print method returns Printable.no_such_page.

Because the print job does not calculate the exact number of pages until the print is finished, the range of page numbers on the dialog box is not yet initialized [1,9999]. We can pass to the print object by building a Java.awt.print.Book object, or you can calculate the number of pages that need to be printed and pass it to the print object to know exactly how many pages to print.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.