Easy printing of documents in Java

Source: Internet
Author: User
Tags valid

A typical statement about implementing document printing in Java describes a complex process that requires the measurement of fonts, parsing text, and drawing the results into a graphics object. This process seems to be quite difficult to implement, and it is inconsistent with advanced programming methods for document video display. If you want to spend a lot of effort to complete this process, you won't first think of programming in Java.

You don't want to finish the process of measuring, parsing, and drawing yourself, but you want to do this by simply sending the document to an object that can handle everything for you. This article will introduce an object ――documentrenderer, which takes a document as a parameter in a method and processes the specified task to finish printing. For example, using this class to display an HTML document takes two steps: Construct an instance of the Documentrenderer class and send the HTML document as an argument to the print (HTMLDocument) method. The Documentrenderer class is used to handle the overhead required to print the document, including displaying a print dialog box and formatting text.

We designed this documentrenderer class to take advantage of this advanced text performance that is already available in Java. Based on the principles of reusable and extensible classes, we used some existing objects (Java.Swing.Text package use it to format the display results) so that the printed results can be displayed on paper. Designing Documentrenderer in this way allows us to build this class with less than 200 lines of code written in the way discussed earlier.

In addition to writing less code, existing objects in the Documentrenderer implementation also provide some additional functionality to make the class more generic. When we first designed this class, we were just going to print out the HTML document. Adding a few features to print other types of documents is a later idea. This feature was added later in the project when we found that we could print a Rich Text format document by adding about 6 lines of extra code in the code for the HTML print class.

Display Document

Documentrenderer can be used to print several types of documents contained in JEditorPane. We tested this printer class with three types of documents (JEditorPane can be identified by default): Htmldocuments, Plaindocuments, and rich-Text format documents. With just a few minor changes, this class should be able to print out other types of documents contained in JEditorPane.

The Documentrenderer class distinguishes the printed form of a document from its video display. This allows you to format text for specific print results without affecting its on-screen display. Documentrenderer uses the actual size of all printed pages to display text and calculate travel spacing (line break). The class allows scaling (scaling) when the document is not wide enough to display on the printed page.

Documentrenderer is quite intelligent. Page breaks do not divide a single line of statements into two pages. The characters are not cut in half, and the bottom half of the sentence is displayed at the top of the next page as the browser displays the upper part of a sentence at the end of the page while processing the general print. This class can handle a large number of fonts, colors, and small icons. There is no problem with the display of the columns text. For each jeditorpane to be able to display the text characteristics, usually documentrenderer can render this performance on paper.

You can combine documentrenderer into your program with just two lines of code. Create an instance of this class with a constructor with no parameters, and then invoke an appropriate printing method to handle other things. For example, the following code will print this htmldocument, which is a valid instance of the HTMLDocument class:

DocumentRenderer DocumentRenderer = new
DocumentRenderer();
DocumentRenderer.print(htmlDocument);

It shows the User a Print dialog box that allows the user to select a printer, print quantity, and so on, as well as an option to cancel printing.

Plaindocuments uses the print (Plaindocument) method to perform printing in the same way as htmldocuments. Because you cannot access rich Text format documents directly in Java, you must send this type of document to Documentrenderer (by encapsulating it in a jeditorpane), Just like this: Documentrenderer.print (JEditorPane);

The jeditorpane here is a valid instance of JEditorPane, which contains a rich Text format document.

For the convenience of the user, you can call the Documentrenderer Pagedialog method to display a print dialog box so that users can adjust the page size, margin settings, and the printing direction of the paper (orientation). Documentrenderer also provides a way for developers to choose whether to scale the documents that cannot be fully displayed in the print width. We think scaling is usually a better option because it prevents text from being separated at the right margin, but it seems to be more appropriate for the user to choose. This method, named Setscalewidthtofit (Boolean), provides a proportional scaling option. You must be sure to call this scaling and Pagedialog method before calling the Print method.

About Documentrenderer

Documentrenderer is used to perform a print dialog display and start the print operation by using the standardized tools available in the Java Swing Printing API. Since this API is not fully understood in the use of this documentrenderer class, and the API has been described in detail in many places, we will not introduce it here. The source code for the Documentrenderer class also includes the complete documentation for this print logic.

However, perhaps we should explain the process that Documentrenderer uses to position text in a separate printed page so that you understand the improvements that this class provides, which will help you review the logic of the display document that the Java printing process usually follows.

Documents are usually printed in a simple way. First, the document is put into JEditorPane. You can think of the printing process as placing a rectangular box on top of the JEditorPane (which is the same size as the print area of the page) and printing the contents without caring for the outside parts.

The upper edge of the rectangle is flush with the top of the JEditorPane, and the area inside the rectangle box is drawn (paint). If the bottom of the rectangle passes through the text, you don't have to take care of it; characters are separated at the base of the printed page. When the second page is printed, the top edge of the rectangle is moved down to the line occupied by the previous down edge, and the procedure is repeatedly executed. Because the second page begins exactly where the first page ends, the characters that are separated at the bottom of the first page appear at the top of the second page. The following pages are the same.

To avoid disconnecting from the line, Documentrenderer carefully checks the side of the document to determine whether a single text and page fit together completely. This would be better than just putting a rectangular box in the JEditorPane and printing the contents.

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.