Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the java Printable's print method get called multiple times with the same page number?

Tags:

java

printing

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?

like image 200
dennisjtaylor Avatar asked Dec 22 '09 00:12

dennisjtaylor


People also ask

How to print with the same page index multiple times?

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.

How to print content to a printable in Java?

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.

What is a printable interface in Java?

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.

How to print multiple images on one page in Python?

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:


1 Answers

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.

like image 103
ZoogieZork Avatar answered Oct 12 '22 22:10

ZoogieZork