1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
public int print(java.awt.Graphics g, PageFormat pageFormat, int pageIndex)
throws PrinterException {
// Whole bunch of measuring stuff to see what will fit on the given
// page number
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
g2.setColor(Color.black);
int fontHeight = g2.getFontMetrics().getHeight();
int fontDesent = g2.getFontMetrics().getDescent();
// hauteur disponible pour le tableau ?
double tailleEmpied = fontHeight;
double tailleEntete = fontHeight;
double pageHeight = pageFormat.getImageableHeight() - tailleEmpied
- tailleEntete;
// See how much we need to scale the image to fit in the page's width
double pageWidth = pageFormat.getImageableWidth();
double tableWidth = (double) this.getColumnModel()
.getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
}
// Calculate how many pages there will be with this table
double headerHeightOnPage = this.getTableHeader().getHeight() * scale;
double tableWidthOnPage = tableWidth * scale;
double oneRowHeight = (this.getRowHeight() + this.getRowMargin())
* scale;
int numRowsOnAPage = (int) ((pageHeight - headerHeightOnPage) / oneRowHeight);
double pageHeightForTable = oneRowHeight * numRowsOnAPage;
int totalNumPages = (int) Math.ceil(((double) this.getRowCount())
/ numRowsOnAPage);
if (pageIndex >= totalNumPages) {
return NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
int yEmpied = (int) (pageHeight + fontHeight - fontDesent);
g2.drawString(_nomTable, 0, yEmpied);
g2.drawString("Page: " + (pageIndex + 1) + "/" + totalNumPages,
(int) pageWidth - 80, yEmpied);
g2.translate(0f, headerHeightOnPage);
g2.translate(0f, -pageIndex * pageHeightForTable);
// This next line treats the last page as a full page
g2.setClip(0, (int) (pageHeightForTable * pageIndex), (int) Math
.ceil(tableWidthOnPage), (int) Math.ceil(pageHeightForTable));
// Draw the particular page
g2.scale(scale, scale);
this.paint(g2);
g2.scale(1 / scale, 1 / scale);
g2.translate(0f, pageIndex * pageHeightForTable);
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0, (int) Math.ceil(tableWidthOnPage), (int) Math
.ceil(headerHeightOnPage));
g2.scale(scale, scale);
this.getTableHeader().paint(g2); // paint header at top
return Printable.PAGE_EXISTS;
} |
Partager