Java print program design Raiders (transfer)

xiaoxiao2021-03-06  38

Foreword

In our practical work, print function often needs to be implemented. However, due to historical reasons, Java provides printing functionality has been relatively weak. In fact, the initial JDK does not support print until JDK1.1 introduces very lightweight print support. Therefore, in the previous program designed by Java / Applet / JSP / Servlet, more complex printing is made by calling an ActiveX / OCX control or VB / VC program. In fact, Sun also has been committed to the improvement of Java printing, and the Java2 platform finally has a robust print mode, which is integrated with the Java2D graphics package. More encouraging, the newly released JDK1.4 provides a complete "Java Print Service API", which is a positive supplement to existing print features. With it, we can achieve most practical applications, including print text, graphics, files, and print previews. This article will explain how to design a Java print program through a specific program example to implement these functions, and analyze the different versions of implementation methods. I hope that you can get some useful tips.

Printing in Java

1, Java's printing API

Java's printing API mainly exists in a java.awt.print package. The new class of JDK1.4 is mainly present in the Javax.Print package and its corresponding subcaps javax.print.event and javax.print.attribute. The Javax.Print package mainly contains related classes of the print service, while javax.print.event contains the definition of the print event, Javax.print.attribute, includes a list of available properties of the print service.

2, how to implement print

To generate a print, at least two:

Need a print service object. This can be implemented in three ways: the version before JDK1.4, you must implement the java.awt.print.printable interface or through Toolkit.GetDefaultToolkit (). GetPrintJob to get the print service object; in JDK1.4, you can pass Javax.print.printSerivCelookup to find a print service object.

Need to start a print job. This also has several implementation methods: You can start printing in java.awt.print.printJob before JDK1.4, now it is rarely used, and now you can also pass Java.Awt .print.printerjob's PrintDialog Displays the print dialog box and then starts printing by the Print method; in JDK1.4, you can display the print dialog through the Javax.Print.Serviceui's PrintDialog, and then call the print method to start a print job.

3, printer dialog

3.1 Printable print dialog

Before you start printing, you can display a print dialog with PrinterJob.PrintDialog. It gives the user a chance to select the range range that should be printed and can be used to change the print settings. It is a local dialog.

In fact, when a print job is performed from a printable object, the print object does not know how many pages needed to print. It just keeps calling the Print method. As long as the print method returns a printable.page_exists value, the print job will stop generating a print page until the print method returns printable.no_such_page, the print job stops.

Since the print job is calculated only after the print is completed, the page number on the dialog is not initialized [1,9999]. We can pass to the print object by building a java.awt.print.book object; you can also calculate the number of pages you need to print through the specified format and passed to the print object, which accurately knows how many pages are to print. 3.2 ServiceUi's Print dialog

Unlike Printable dialogs, the default behavior of the JDK1.4 provides the default behavior of the serviceUI's printer dialog that has been changed with the new API: By default, the dialog is not displayed. We must call the PrintDialog method using the ServiceUI class to create the print dialog as shown below.

Java printing program design instance

1, print text

1.1 application scenario

Suppose we need to print a text editing domain (may have only a few lines, may also contain multiple pages), and print up to 54 per page, how to implement it?

1.2 solution

Basic ideas are as follows: First we need to implement the Printable interface, then calculate how many pages in the format of up to 54 rows per page, and perform the corresponding print action when printing the button is clicked. The specific operation of printing text can be implemented via Graphics2D's DrawString method.

1) Implement Printable interface

/ * Graphic specifies the printing graphics environment; PageFormat indicates the printing page format (the page size is a metering unit, 1 point of 1 inch 1/72, 1 inches is 25.4 mm. A4 paper is approximately 595 × 842 points); Designation page number * /

Public Int Print (Graphics G, Pageformat Pf, INT Page) throws printerexception {

Graphics2D G2 = (Graphics2D) g;

g2.setpaint (color.black); // Setting the print color is black

IF (Page> = PAGES) // When the print page number is larger than the total number of pages that need to be printed, the print job ends

Return Printable.no_such_page;

g2.translate (pf.getimageablex (), pf.getimageable ()); // Convert coordinate, determine print borders

DrawCurrentPageText (G2, PF, PAGE); // Print current page text

Return Printable.page_exists; // Continue print jobs when there is a print page

}

/ * Print the specific text content of the specified page number * /

Private Void DrawCurrentPageText (Graphics2D G2, PageFormat Pf, Int Page) {

String s = getdrawtext (printstr) [Page]; // Get the contents of the current page to print text

/ / Get the default font and the corresponding size

FontrenderContext context = g2.GetFontrenderContext ();

FONT f = area.getfont ();

String drawtext;

FLOAT Ascent = 16; // Givert characters

INT K, I = F.Getsize (), lines = 0;

While (S.Length ()> 0 && Lines <54) // Limited each page within 54 lines

{

K = s.indexof (/

); // Get the location of each carriage

IF (k! = -1) // There is a carriage return

{

LINES = 1; // Calculate the number of lines

DrawText = s.substring (0, k); // Get every line of text

g2.drawstring (DrawText, 0, Ascent); // Specific printing every line of text, while paper shift IF (S.Substring (k 1) .length ()> 0) {

s = s.substring (k 1); // Intercepting the text that has not yet printed

ascent = i;

}

}

Else // does not exist

{

LINES = 1; // Calculate the number of lines

DrawText = S; // Get every line of text

g2.drawstring (DrawText, 0, Ascent); // Specific printing every line of text, while paper shift

s = ""; // Text has ended

}

}

}

/ * Press the print target text as a string array * /

Public string [] getdrawtext (string s) {

String [] drawtext = new string [pages]; // According to the number of pages

For (int i = 0; i

DrawText [i] = ""; // array element is initialized to empty strings

INT K, SUFFIX = 0, LINES = 0;

While (s.ley "> 0) {

IF (LINES <54) // Not enough

{

K = s.indexof (/

);

IF (k! = -1) // There is a carriage return

{

LINES = 1; // Route number

/ / Calculate the specific text content of the page, store the array element to the corresponding subscript

Drawtext [suffix] = DrawText [SUFFIX] S.SUBSTRING (0, K 1);

IF (s.SUBSTRING (K 1) .length ()> 0)

s = s.substring (k 1);

}

Else

{

LINES = 1; // Route number

// Put the text content to the corresponding array element

DrawText [SUFFIX] = DrawText [SUFFIX] S;

s = "";

}

}

Else // is full

{

LINES = 0; // Route statistics

SUFFIX ; // array subtracted 1

}

}

Return DrawText;

}

2) Calculate the total number of pages that need to print

Public int getpagescount (String Curstr) {

INT Page = 0;

INT position, count = 0;

String str = curstr;

While (str.Length ()> 0) // The text has not been calculated

{

Position = str.indexof (/

); // calculate the location of the carriage return

Count = 1; // Statistics

IF (position! = -1)

Str = str.substring (position 1); // Interception has not been calculated text

Else

Str = ""; // Text has been calculated

}

IF (count> 0)

Page = count / 54 1; // Remove the total number of pages with 54 in total

Return page; // Return to the total number of pages you need to print

}

Implement the print action button in JDK1.4, and complete the specific print operation

Private void printTextAction () {

Printstr = area.getText (). Trim (); // Get the target text IF you need to print (PrintStr! = null &&printstr.length ()> 0) // When the print content is not empty

{

Pages = getpagescount (Printstr); // Get the number of print pages

PrinterJob MyPRTJOB = PrinterJob.getPrinterJob (); // Get the default print job

PageFormat PageFormat = MyPRTJOB.DEFAULTPAGE (); // Get the default print page format

MyPRTJob.SetPrintable (this, PageFormat); // Setting the print job

IF (MyPRTJob.PrintDialog ()) // Displays the print dialog

{

Try {

MyPRTJob.print (); // Specific print operations for each page

}

Catch (Printerexception PE) {

PE.PrintStackTrace ();

}

}

}

Else {// If the print content is empty, prompt user printing will cancel

JOPTIONPANE.SHOWCONFIRMDIALOG (NULL, "Sorry, Printer Job Is Empty, Print Cancelle!", "EMPTY", JOPANE.DEFAULT_OPTION, JOPANE.WARNING_MESSAGE

}

}

The API provided in JDK1.4 is available to implement the print action button to listen, and complete the specific print operation.

Private void printText2action () {

Printflag = 0; // Print flag clear zero

Printstr = area.getText (). Trim (); // Get the target text you need to print

IF (PrintStr! = null &&printstr.length ()> 0) // When the print content is not empty

{

Pages = getpagescount (Printstr); // Get the number of print pages

/ / Specify the printout format

Docflavor flavor = docflavor.service_formatted.printable;

/ / Positioning the default print service

PrintService PrintService = PrintServiceLookup.lookupdefaultprintService ();

// Create a print job

DocprintJob Job = printService.createPrintJob ();

/ / Set the print properties

PrintRequestAttributeSet Pras = new hashprintrequestattributeset ();

DOCATTRIBUTESET DAS = new hashdocattributeset ();

/ / Specify print content

DOC DOC = New SimpleDoc (this, flavor, das);

/ / Do not display the print dialog, directly print jobs

Try {

Job.print (DOC, PRAS); / / Specific print operation per page

}

Catch (PrintException PE) {

PE.PrintStackTrace ();

}

}

Else {// If the print content is empty, prompt user printing will cancel

JOPTIONPANE.SHOWCONFIRMDIALOG (NULL, "Sorry, Printer Job Is Empty, Print Cancelle!", "EMPTY", JOTIONPANE.DEFAULT_OPTION, JOPANE.WARNING_MESSAGE);

}

Printing preview

1, application scenario

Most commercial applications require print preview mechanisms that allow us to see the page on the screen, so you will wast paper because you don't like print results. Suppose we need to print preview before printing the text mentioned by the previous section. So how do you implement it? The interface implementation is shown below: (Next Preview Next page, Preview Preview Previous page, Close Close Preview)

2, solution

Basic ideas: Although the Java2 platform prints API does not provide a standard print preview dialog, it is not complicated to design. Normally, the Print method plots the page environment into a printer graphics environment to implement print. In fact, the Print method does not really produce a print page, which just draws the content to the graphical environment. Therefore, we can ignore the screen graphics environment, through the appropriate scale, so that the entire print page is hosted in a screen rectangle to achieve accurate print preview.

In the design implementation of the print preview, it is mainly necessary to solve two problems.

First, how to draw the print content to the screen in a suitable scale;

Second, how to implement the front and reappere.

Below I gave the specific implementation method of these two questions, please refer to the PrintPreviewDialog.java file in the attachment.

/ * Draw to the screen * /

Public void PaintComponent (graphics g) {

Super.PaintComponent (g);

Graphics2D G2 = (Graphics2D) g;

Pageformat pf = printerJob.getPrinterJob (). DefaultPage (); // Get page format

Double Xoff; // Level offset at the initial position of the page on the screen

Double Yoff; // Vertical offset on the initial position of the page on the screen

Double scale; // Passed the page on the screen

Double PX = pf.getwidth (); // page width

Double py = pf.getHeight (); // page height

Double SX = getWidth () - 1;

Double Sy = GetHeight () - 1;

IF (PX / PY

Scale = SY / PY; / / Calculation ratio

XOFF = 0.5 * (SX - scale * px); // horizontal offset

YOFF = 0;

}

Else {

Scale = SX / PX; // Calculation ratio

XOFF = 0;

YOFF = 0.5 * (Sy - scale * py); // Vertical offset

}

G2.Translate ((float) Xoff, (float) yoff; // conversion coordinates

G2.Scale ((Float) Scale, (Float) scale;

Rectangle2D Page = New Rectangle2D.double (0, 0, PX, PY); // Draw a page rectangle

G2.SETPAINT (color.white); // Settings page background is white

g2.fill (page);

g2.setpaint (color.black); // Settings page text is black

G2.DRAW (PAGE);

Try {

Preview.Print (G2, PF, CURRENTPAGE); // Displays the specified preview page}

Catch (Printerexception PE) {

G2.draw (new line2d.double (0, 0, px, py));

g2.draw (new line2d.double (0, px, 0, py));

}

}

/ * Preview the specified page * /

Public void viewpage (int POS) {

INT newPage = CurrentPage POS; // Specify the page in the actual range

IF (0 <= newpage& newpage

CurrentPage = newPage; // assigns the specified page to the current page

Repaint ();

}

}

This way, when pressing the "Next" button, only need to call canvas.viewPage (1); when pressing the "Preview" button, you only need to call canvas.viewPage (-1) to turn the front and reap pages of the preview.

Print graphics

1, application scenario

In practical applications, we also need to print graphics. For example, we sometimes need a full interface of a Java Applet or a application form and all components therebet all the components, how should it be implemented?

2, solution

The basic ideas are as follows: All the Print and Printall methods are provided in the Java's Component class and its derived class. These two methods can be called directly to the components and graphics as long as they set the properties.

/ * Print the specified form and the components therebet * /

Private void printfraMection () {

Toolkit Kit = Toolkit.GetDefaultToolkit (); // Get Toolbox

Properties PROPS = New Properties ();

Props.Put ("AWT.PRINT.PRINTER", "DURANGO"); // Setting the print properties

Props.Put ("AWT.PRINT.NUMCOPIES", "2");

IF (kit! = null) {

/ / Get the print object comes with the toolbox

PrintJob PrintJob = Kit.GetPrintJob (this, "Print Frame", PrOPS;

IF (PrintJob! = NULL) {

Graphics pg = printjob.getgraphics (); // Get the graphics environment of the print object

IF (pg! = null) {

Try {

this.printall (PG); // Print the form and all of its components

}

Finally {

pg.dispose (); // Logout graphics environment

}

}

PrintJob.end (); // End print job

}

}

}

print documents

1, application scenario

In many practical applications, we may need to print a file specified by the user. This file may be graphical files such as GIF, JPEG, etc. may also be text files, such as txt, java files, etc.; may also be complex PDF, DOC files, etc. So how should we achieve this for such print demand?

2, solution

Basic Ideas: In JDK1.4, it is very troublesome and complicated to achieve such printing functions, even unimaginable. But fortunately, JDK1.4's print service API provides a set of classes and methods for printing file streams. With them, we can make it easy and easily to achieve a variety of different types of file printing. A universal processing method is given below. / * Print the specified file * /

Private void printfileAction () {

/ / Construct a file selector, default is the current directory

Jfilechooser filechooser = new jfilechooser (systemproperties.user_dir);

INT State = filechooser.showopendialog (this); // Popup file selection dialog

if (state == filechooser.Approve_option) // If the user selects the file

{

File file = filechooser.getSelectedFile (); // Get selected files

/ / Construct the print request attribute set

PrintRequestAttributeSet Pras = new hashprintrequestattributeset ();

/ / Set the print format, because the file type is not determined, here is autosense

Docflavor flavor = docflavor.input_stream.autosense;

/ / Find all available print services

PrintService PrintService [] = PrintServiceLookup.lookuppprintServices (Flavor, PRAS)

/ / Positioning the default print service

PrintService DefaultService = PrintServiceLookup.lookupdefaultprintService ();

// Display print dialog

PrintService Service = Serviceui.PrintDialog (Null, 200, 200, PrintService, DefaultService, Flavor, PRAS)

IF (service! = null) {

Try {

DocprintJob Job = service.createprintJob (); // Create a print job

FileInputStream Fis = New fileInputStream (file); // Configure the file stream to be printed

DOCATTRIBUTESET DAS = new hashdocattributeset ();

DOC DOC = New SimpleDoc (Fis, Flavor, DAS); // Establish a print file format

Job.print (DOC, PRAS); // Printing

}

Catch (Exception E) {

E.PrintStackTrace ();

}

}

}

}

In the above example, the type of file has not been determined, the print format of the specified file is defined as DOCFLAVOR.INPUT_STREAM.AUTOSENSE. In fact, if you have confirmed the format of the file before printing, if GIF, you should be defined as DOCFLAVOR.INPUT_STREAM.GIF; if it is PDF, it should be defined as docflavor.input_stream.pdf; as pure ASCII Files, you can define DOCFLAVOR.INPUT_STREAM.TEXT_HTML_US_ASCII. and many more.

JAVAX.Print.docflavor of JDK1.4 provides an extremely rich file stream type, which you can make appropriate options depending on the specific application needs. The specific API reference documentation can be seen in this article 3. Conclude

The above is some of the experiences in the J2EE application development, summarizing some experiences with Java printing programming, hoping to give you some revelations and benefits. Although the print function is currently used in Java, it is still more trouble compared with Microsoft's MFC API. However, JDK1.4 is launched, and the printed print function before Java is an excellent complement. I believe that if you can understand the print program design examples described above and apply and expand, you should be able to solve the actual programming issues of currently most applications. With the further development and improvement of Java, it will better enrich its basic class libraries and print APIs. I believe that advanced printing functions to implement Java will also become more and more increasingly becoming a headache of our Java obsesses.

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

New Post(0)