View RSS Feed

Java AWT

Printing a Multiple Page Document

Rate this Entry
by , 03-07-2012 at 05:11 PM (1985 Views)
To print number of graphic images, page index is used so that to iterate via pages & print just 1 at every page. E.g, many images are presented in this array:
Java Code:
BufferedImage[] images = new BufferedImage[10];
Then, use the print() method as in the following code fragment:
public int print(Graphics graphics,
           PageFormat pageFormat, int pageIndex)
           throws PrinterException {

    if (pageIndex < images.length) {
                           100, 100, null);
        return PAGE_EXISTS;
    } else {
        return NO_SUCH_PAGE:
If there exists a continuous document than it shall be possible for the application to calculate that how much content could be made possible to get fit on each page. Break page at that point. Number of lines that could get fit on page shall be calculated by the application in case text document has many lines. A point is created by the Point class which represent a location in (x,y)
Height of a single line of text can be calculated by using the FontMetrics class.
Java Code:
Font font = new Font("Serif", Font.PLAIN, 10);
FontMetrics metrics = graphics.getFontMetrics(font);
int lineHeight = metrics.getHeight();
printable area of the page is described by PageFormat parameter. Vertical span of the page is found by using this code fragment:
Java Code:
double pageHeight = pageFormat.getImageableHeight();
No. of lines that might get fixed on a page & the no. of page breaks can be calculated by using this code.
Java Code:
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; 

Submit "Printing a Multiple Page Document" to Facebook Submit "Printing a Multiple Page Document" to Digg Submit "Printing a Multiple Page Document" to Submit "Printing a Multiple Page Document" to StumbleUpon Submit "Printing a Multiple Page Document" to Google