Java Implementation printing function

Source: Internet
Author: User
Tags print object
Preface

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 demonstrate how to design a Java print program to implement these functions through a specific program instance, and compare the implementation methods of different versions. I hope you can get some helpful hints from it. Printing in Java 2.1 Java Print 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.2 How to achieve printing

To produce a print, there are at least two reasons to consider: A Print service object is required. This can be done in three ways: Prior to jdk1.4, the Java.awt.print.Printable interface must be implemented or Toolkit.getdefaulttoolkit (). Getprintjob to obtain the Print service object; jdk1.4 can also be found by Javax.print.PrintSerivceLookup to locate a Print service object. 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, and now rarely used); The Java.awt.print.PrinterJob PrintDialog displays the Print dialog box, and then starts printing by using the Print method, which in the jdk1.4 can be displayed through the Javax.print.ServiceUI printDialog Print dialog box, and then call the Print method to start a printing job. 2.3 Printer dialog box

2.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.

2.3.2 Serviceui Print dialog box

Unlike the printable dialog box, the default behavior of the Printer dialog box that provides Serviceui in jdk1.4 has been changed with the new API: the dialog box is not displayed by default. We must use the Serviceui class to invoke the PrintDialog method to create a print dialog box as shown below. Java Print Programming instance 3.1 printing text

3.1.1 Application Scenario

Let's say we need to print the contents of one of the text-editing fields (maybe only a few lines, possibly multiple pages) of a form, and print up to 54 lines per page.

3.1.2 Resolution Method

The basic idea is as follows: first we need to implement the printable interface, and then according to the format of up to 54 lines per page to calculate the total number of pages to print, when the text button is clicked, the corresponding print action. The concrete operation of the printed text can be realized by graphics2d drawstring method.

1. Implement printable interface

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21

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.