You have already learned how to use the
Printable
interface to print a single page document.
However, documents are usually more than one physical page in length.
Pagination is the process of identifying the location in a document where
page breaks and printing accordingly.
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:
BufferedImage[] images = new BufferedImage[10];
print()
method as shown in the following code
fragment:
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex < images.length) { graphics.drawImage(images[pageIndex], 100, 100, null); return PAGE_EXISTS; } else { return NO_SUCH_PAGE: } }
Point
class creates a point representing a location in (x,y)
To calculate the height of a single line of text, use the
FontMetrics
class.
Font font = new Font("Serif", Font.PLAIN, 10); FontMetrics metrics = graphics.getFontMetrics(font); int lineHeight = metrics.getHeight();
The PageFormat
parameter describes the
printable area of the page. In particular, to find the vertical
span of the page use the following code fragment:
double pageHeight = pageFormat.getImageableHeight();
int linesPerPage = ((int)pageHeight)/lineHeight); int numBreaks = (textLines.length-1)/linesPerPage; int[] pageBreaks = new int[numBreaks]; for (int b=0; b < numBreaks; b++) { pageBreaks[b] = (b+1)*linesPerPage; }
Use the print()
method to calculate the printable area for the
following reasons:
FontRenderContext
and this is implicit in the FontMetrics
object returned by
the printer graphics which is not available except inside the
print()
method.PageFormat
object passed into
the print()
method provides this information./* Draw each line that is on this page. * Increment 'y' position by lineHeight for each line. */ int y = 0; int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex-1]; int end = (pageIndex == pageBreaks.length) ? textLines.length : pageBreaks[pageIndex]; for (int line=start; line<end; line++) { y += lineHeight; g.drawString(textLines[line], 0, y); }
PaginationExample.java
.
The following simplifying factors are used in the PaginationExample
code: