English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

- i have to write a database accessing program in java
- i need to create a report (extracting data from the database) and print it in an A4 paper format
- i use a JDBC conexion on MySQL
- i can retrive the date, but i do not know ho to format it to fit an A4 paper
- i do not know the actual command for printing from java (System.out.println() is for the display; what is for the printer?)

2006-07-23 23:31:25 · 4 answers · asked by Ovidiu B 1 in Computers & Internet Programming & Design

4 answers

It's not easy to do, and not something I've actually done myself, however this link seems to be what you're looking for:
http://java.sun.com/j2se/1.4.2/docs/guide/jps/spec/printing.fm2.html

2006-07-24 02:12:41 · answer #1 · answered by Brandon T 2 · 1 0

Ovidiu B will this help

window.print()

Example
Click to Print This Page

You can set it to trigger off a button:





And, if you really want to be annoying, off of just about any Event Handler like this, adding onLoad will force a print request when the page loads:

onLoad="window.print()"

Okay, now you have the power to force a print request, but that doesn't mean to simply offer a print on any page. You should make a point of being helpful to your users.

2006-07-23 23:40:10 · answer #2 · answered by Joe_Young 6 · 0 0

Java printing system
The Java printing system has evolved considerably in its last two releases. Starting with version 1.2, the printing system allows you to use the Java 2D API -- one of the most advanced graphical APIs built as part of a programming language -- to render a page. This 2D API allows whatever is drawn on the screen to be rendered on paper.

Although more advanced now, the printing API still only supports the printer currently selected by the user at any given time. Java does not support printer discovery -- obtaining a list of available printers and their features on a given computer. Available printers can either be local or networked. When using the API, no way exists for obtaining a printer list programmatically; only if the print dialog is displayed can the user select a printer. This is a feature that Sun, which is adhering to the Internet Printing Protocol, will address in the next version of Java (1.4).

The printing model changed completely in Java 1.2. In previous versions of Java, the rendering process was not optimized at all. In Java 1.1 for example, printing a simple page required a great deal of memory and was very slow. Java 1.2 streamlined and optimized the rendering process. This redesigned API is based on a callback model, in which the printing subsystem, not your program, controls when a page is rendered. This model is more object-oriented in nature than the one used in JDK 1.1, in which the application was in charge of the printing process.

To simplify the concept, let's say that your program has a contract with the printing subsystem to supply a given page at a given time. The printing subsystem may request that your application render a page more than once, or render pages out of sequence. This model provides several advantages. First, by sending strips of the page instead of the whole page to the printer, it allows the application to print complex documents that would require more printer memory than is available. The application does not have to know how to print each strip; it only needs to know how to render a given page. The API will take care of the rest. In this case, the printing subsystem might request that a page be rendered several times depending on the number of strips required to completely print the page. Second, if the paper tray on a particular printer outputs the pages in reverse order, then your application might be asked to print the document in reverse order, so it will appear in the right order in the output tray.

Rendering models
There are two printing models in Java: Printable jobs and Pageable jobs.


Printables
Printable jobs are the simpler of the two printing models. This model only uses one PagePainter for the entire document. Pages are rendered in sequence, starting with page zero. When the last page prints, your PagePainter must return the NO_SUCH_PAGE value. The print subsystem will always request that the application render the pages in sequence. As an example, if your application is asked to render pages five through seven, the print subsystem will ask for all pages up to the seventh page, but will only print pages five, six, and seven. If your application displays a print dialog box, the total number of pages to be printed will not be displayed since it's impossible to know in advance the number of pages in the document using this model.

Pageables
Pageable jobs offer more flexibility than Printable jobs, as each page in a Pageable job can feature a different layout. Pageable jobs are most often used with Books, a collection of pages that can have different formats. I will explain the Book class in a moment.

A Pageable job has the following characteristics:



Each page can have its own painter. For example, you could have a painter implemented to print the cover page, another painter to print the table of contents, and a third to print the entire document.

You can set a different page format for each page in the book. In a Pageable job, you can mix portrait and landscape pages.

The print subsystem might ask your application to print pages out of sequence, and some pages may be skipped if necessary. Again, you don't have to worry about this as long as you can supply any page in your document on demand.

The Pageable job doesn't need to know how many pages are in the document.
Books
Also new since version 1.2 is the Book class. This class allows you to create multiple-page documents. Each page can have its own format and its own painter, giving you the flexibility to create sophisticated documents. Since the Book class implements the Pageable interface, you could implement your own Book class when the provided Book class lacks the features that you require.

A Book class represents a collection of pages. When first created, the Book object is empty. To add pages, you simply use one of the two append() methods (see my explanation of this class in the API section for more details). This method's parameters are the PageFormat object, which defines the physical characteristics of the page, and a PagePainter object, which implements the Printable interface. If you don't know the number of pages in your document, simply pass the UNKNOWN_NUMBER_OF_PAGES value to the append() method. The printer system will automatically find the number of pages by calling all the page painters in the book until it receives a NO_SUCH_PAGE value.

2006-07-24 17:01:31 · answer #3 · answered by ihoston 3 · 0 0

Hi,

Use io.FileOutputStream Object. You will find 'Print' Method.

You can use line count to format the paper while printing or use any templates (templates objects, downloadable).

Thanks

2006-07-23 23:40:10 · answer #4 · answered by riya 1 · 0 0

fedest.com, questions and answers