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
|
/** {@docRoot}Gestion impression */
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black);
int fontHeight = g2.getFontMetrics().getHeight();
int fontDescent = g2.getFontMetrics().getDescent();
//double pageHeight = pageFormat.getImageableHeight()-fontHeight;
double pageHeight = pageFormat.getImageableHeight();
double pageWidth = pageFormat.getImageableWidth();
//double pageHeight = pageFormat.getHeight();
//double pageWidth = pageFormat.getWidth();
double tableWidth = (double)grille.getColumnModel().getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth)
scale = pageWidth / tableWidth;
double headerHeightOnPage = grille.getTableHeader().getHeight()*scale;
double tableWidthOnPage = tableWidth * scale;
double oneRowHeight = (grille.getRowHeight() + (grille.getRowMargin()+1))*scale;
int numRowsOnAPage = (int)((pageHeight-headerHeightOnPage)/ oneRowHeight);
double pageHeightForTable = oneRowHeight* numRowsOnAPage;
int totalNumPages = (int)Math.ceil(((double)grille.getRowCount())/ numRowsOnAPage);
if (pageIndex >= totalNumPages) return Printable.NO_SUCH_PAGE;
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2.drawString("Page: " + (pageIndex+1),(int)pageWidth/2-35, (int)(pageHeight+ fontHeight- fontDescent));
g2.translate(0f, headerHeightOnPage);
g2.translate(0f, -pageIndex*pageHeightForTable);
if (pageIndex + 1 == totalNumPages) {
int lastRowPrinted = numRowsOnAPage*pageIndex;
int numRowsLeft = grille.getRowCount()-lastRowPrinted;
g2.setClip(0, (int)(pageHeightForTable*pageIndex),(int)Math.ceil(tableWidthOnPage),(int)Math.ceil(oneRowHeight* numRowsLeft)); }
else {
g2.setClip(0, (int)(pageHeightForTable*pageIndex),(int)Math.ceil(tableWidthOnPage),(int)Math.ceil(pageHeightForTable));}
g2.scale(scale, scale);
grille.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.setClip(0, 0,(int)Math.ceil(tableWidthOnPage),0);
g2.scale(scale, scale);
return Printable.PAGE_EXISTS; } |
Partager