📄 printutilitiesrev1.java
字号:
import java.awt.*;import java.awt.print.*;import javax.swing.*;import javax.swing.border.Border;public class PrintUtilitiesRev1 implements Printable { JTable tableView; String header="",pageOrientation="landscape"; private Component printcomponent; public static void printComponent(Component c, String header, String pageOrientation) { new PrintUtilitiesRev1(c,header,pageOrientation).print(); } public PrintUtilitiesRev1(Component printcomponent,String header, String pageOrientation) { this.printcomponent = printcomponent; this.header = header; //this.footer = footer; this.pageOrientation = pageOrientation; } public void print() { try{ PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(this); if (printJob.printDialog()) try { printJob.print(); } catch (PrinterException pe) { System.out.println("Error in printing: " + pe); } }catch(Exception e){ e.printStackTrace(); } } public int print(Graphics g, PageFormat pageFormat, int pageIndex) { try{ if (this.printcomponent instanceof JTable) { JTable tableView = (JTable)this.printcomponent; Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.black); int fontHeight = g2.getFontMetrics().getHeight()+100; int fontDesent = g2.getFontMetrics().getDescent(); //(for printing footer on a page) if(pageOrientation.equalsIgnoreCase("potrait")){ pageFormat.setOrientation(pageFormat.PORTRAIT); double pageHeight = pageFormat.getImageableHeight() - fontHeight; System.out.println("potrait page height....."+pageHeight); double pageWidth = pageFormat.getImageableWidth(); double tableWidth = (double) tableView.getColumnModel(). getTotalColumnWidth(); double scale = 1; if (tableWidth >= pageWidth) { scale = pageWidth / tableWidth; } //for setting table header height double headerHeightOnPage = (tableView.getTableHeader().getHeight()) * scale; //for setting table width double tableWidthOnPage = tableWidth * scale; //for setting each row height double oneRowHeight = (tableView.getRowHeight() + tableView.getRowMargin()) * scale; int numRowsOnAPage = (int) ( (pageHeight - headerHeightOnPage) / oneRowHeight); double pageHeightForTable = oneRowHeight * numRowsOnAPage; int totalNumPages = (int) Math.ceil( ( (double) tableView.getRowCount()) / numRowsOnAPage); Border tableBorder = BorderFactory.createLineBorder(Color.black); tableView.setBorder(tableBorder); g2.translate(pageFormat.getImageableX()+30, pageFormat.getImageableY()+10); //for printing footer (Page number) on the page PrintHeaderFooter footer = new PrintHeaderFooter("","","Page"); if(footer.trim().length() > 0){ g2.drawString(footer, (int) pageWidth /2-35, (int) (pageHeight + fontHeight - fontDesent)-10); //bottom left } //for printing header on center of the page if(header.trim().length() > 0){ int fontWidth = g2.getFontMetrics().stringWidth(header); g2.drawString(header, ((int) pageWidth / 2) - (int)(fontWidth/2), -1); //header center } g2.translate(0f, headerHeightOnPage+5); g2.translate(0f, -pageIndex * pageHeightForTable); //To set the appropriate bounds, //if the piece of the table is smaller //than the size available, if (pageIndex + 1 == totalNumPages) { int lastRowPrinted = numRowsOnAPage * pageIndex; int numRowsLeft = tableView.getRowCount() - lastRowPrinted; g2.setClip(0, (int) (pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(oneRowHeight * numRowsLeft)); } //or print the entire area available. else { g2.setClip(0, (int) (pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(pageHeightForTable)); } g2.scale(scale,scale); tableView.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); tableView.getTableHeader().paint(g2);//paint header at top if (pageIndex >= totalNumPages) { return NO_SUCH_PAGE; } } else { pageFormat.setOrientation(pageFormat.LANDSCAPE); double pageHeight = pageFormat.getImageableHeight() - fontHeight; System.out.println("landscape page height....."+pageHeight); double pageWidth = pageFormat.getImageableWidth(); double tableWidth = (double) tableView.getColumnModel().getTotalColumnWidth(); double scale = 1; if (tableWidth >= pageWidth) { scale = pageWidth / tableWidth; } //for setting table header height double headerHeightOnPage = (tableView.getTableHeader().getHeight()) * scale; //for setting table width double tableWidthOnPage = tableWidth * scale; //for setting each row height double oneRowHeight = (tableView.getRowHeight() + tableView.getRowMargin() + 1) * scale; int numRowsOnAPage = (int) ( (pageHeight - headerHeightOnPage) / oneRowHeight); double pageHeightForTable = oneRowHeight * numRowsOnAPage; int totalNumPages = (int) Math.ceil( ( (double) tableView.getRowCount()) / numRowsOnAPage); g2.translate(pageFormat.getImageableX()+20, pageFormat.getImageableY()+20); //for printing footer on the page if(footer.trim().length() > 0){ g2.drawString(footer, (int) pageWidth / 2 - 35, (int) (pageHeight + fontHeight - fontDesent)-10); //bottom left } //for printing header on center of the page if(header.trim().length() > 0){ int fontWidth = g2.getFontMetrics().stringWidth(header); g2.drawString(header, ((int) pageWidth / 2) - (int)(fontWidth/2), -5); //header center } g2.translate(1f, headerHeightOnPage); g2.translate(1f, -pageIndex * pageHeightForTable); //To set the appropriate bounds, //if the piece of the table is smaller //than the size available, if (pageIndex + 1 == totalNumPages) { int lastRowPrinted = numRowsOnAPage * pageIndex; int numRowsLeft = tableView.getRowCount() - lastRowPrinted; System.out.println("rows left---------------->"+numRowsLeft); g2.setClip(0, (int) (pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(oneRowHeight * numRowsLeft)); } //or print the entire area available. else { g2.setClip(0, (int) (pageHeightForTable * pageIndex), (int) Math.ceil(tableWidthOnPage), (int) Math.ceil(pageHeightForTable)); } g2.scale(scale,scale); tableView.paint(g2); g2.scale(1/scale,1/scale); g2.translate(1.2f,pageIndex*pageHeightForTable); g2.translate(1.2f, -headerHeightOnPage); g2.setClip(0, 0,(int) Math.ceil(tableWidthOnPage), (int)Math.ceil(headerHeightOnPage)); g2.scale(scale,scale); tableView.getTableHeader().paint(g2);//paint header at top if (pageIndex >= totalNumPages) { return NO_SUCH_PAGE; } } return Printable.PAGE_EXISTS; } /* else if(this.printcomponent instanceof JLabel) { if (pageIndex > 0) { return (NO_SUCH_PAGE); } else { Graphics2D g2d = (Graphics2D) g; //printing as per page orientation if(pageOrientation.equalsIgnoreCase("potrait")){ pageFormat.setOrientation(pageFormat.PORTRAIT); }else{ pageFormat.setOrientation(pageFormat.LANDSCAPE); } g2d.translate(pageFormat.getImageableX()+75, pageFormat.getImageableY()); int fontWidth = g2d.getFontMetrics().stringWidth(header); int pageWidth = (int)pageFormat.getImageableWidth(); int pageHeight = (int)pageFormat.getImageableHeight(); System.out.println("pagewidth...."+pageWidth); System.out.println("printcomponent width..."+printcomponent.getWidth()); int fontHeight = g2d.getFontMetrics().getHeight()+100; //+50 int fontDesent = g2d.getFontMetrics().getDescent(); //setting header in center after translation/moving if(header.trim().length() > 0){ g2d.drawString(header, ((int) pageWidth / 2) - (int)(fontWidth/2), 15); //header center } if(footer.trim().length() > 0){ g2d.drawString(footer, ((int) pageWidth / 2) - (int)(fontWidth/2), (int) (pageHeight-15)); //bottom left } g2d.translate(pageFormat.getImageableX()+140, pageFormat.getImageableY()+70); //disableDoubleBuffering(printcomponent); printcomponent.paint(g2d); //enableDoubleBuffering(printcomponent); return (PAGE_EXISTS); } } else if(this.printcomponent instanceof JPanel){ if (pageIndex >= 1) { return Printable.NO_SUCH_PAGE; } if(pageOrientation.equalsIgnoreCase("potrait")){ pageFormat.setOrientation(PageFormat.PORTRAIT); }else{ pageFormat.setOrientation(PageFormat.LANDSCAPE); } Graphics2D g2 = (Graphics2D) g; double height = pageFormat.getImageableHeight(); double width = pageFormat.getImageableWidth(); int fontWidth = g.getFontMetrics().stringWidth(header); g2.translate(pageFormat.getImageableX()+10, pageFormat.getImageableY()+10); g2.setColor(Color.black); if(header.trim().length() >0){ g.drawString(header, ( (int) width / 2) - (int) (fontWidth / 2), 0); //header center } if(footer.trim().length() >0){ g2.drawString(footer, (int) width / 2, (int) height - g2.getFontMetrics().getHeight()); } g2.translate(150f, 160f); g2.setClip(0, 0, (int) width, (int) (height - g2.getFontMetrics().getHeight() * 2)); printcomponent.paint(g2); return Printable.PAGE_EXISTS; }*/ }catch(Exception e){ e.printStackTrace(); } return (PAGE_EXISTS); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -