From sun's documentation
"The printing system might request that a page be rendered multiple times before moving to the next page."
The examples always show something like this:
Printable print(Graphics g, PageFormat pageFormat, int page) {
if (page == 0)
do...
else if(page == blah...)
}
If you follow this pattern your code typically works fine because it is explicit based on the page number. Not following this pattern caused me great pain until I realized it was getting called multiple times with the same page number and started to cache the pages.
Why does the java Printable's print method get called multiple times with the same page number?
The print () method may be called with the same page index multiple times until the document is completed. This feature is applied when the user specifies attributes such as multiple copies with collate option. The PageFormat's imageable area determines the clip area.
import java.awt.print.*; PrinterJob job = PrinterJob.getPrinterJob (); Next provide code that renders the content to the page by implementing the Printable interface. An application typically displays a print dialog so that the user can adjust various options such as number of copies, page orientation, or the destination printer.
Interface Printable. public interface Printable The Printable interface is implemented by the print methods of the current page painter, which is called by the printing system to render a page. When building a Pageable, pairs of PageFormat instances and instances that implement this interface are used to describe each page.
In case of printing several graphics images, one per page, use the page index to iterate through these pages and print one on each page. For example, if several images are represented in the following array: then use the print () method as shown in the following code fragment:
The Java printing system is at the mercy of the underlying OS printing system, and that system may request a single page be rendered multiple times.
One reason is banded printing -- if the printer doesn't have enough memory to render the entire page at once -- in that case, the OS will ask Java for the page again so it can print the page in strips ("bands"). This is the specific case mentioned in the Java 2D Programmer's Guide, in the section "Printing Concepts".
There may be other reasons; it's really up to the OS's printing system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With