java打印表格到打印机
2011-12-12 00:07
351 查看
在这里我我有部分功能是要将一张表格通过java调用打印机打印出来,这里要用到java的Printable接口
在
在这里我通过链表来获取表格内容,以实现动态改变要打印出来的表格的大小,而我在打印方法中是用二维数组来实现打印和定位的,
因此有了个蛋痛的将链表转化为一维数组,然后又将一维数组转化为二维数组的过程。
链表转化为一维数组可以直接用toarray()方法
而一维数组转化为二维数组,int n = 4;//列
int m = osf.getInfoStrings().length/n;//行
String[][] data1 = new String[m]
;
int num = -1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
num++;
data1[i][j] = (String)osf.getInfoStrings()[num];
}
}
重要的实现部分就是,画出表格然后打印
为了使打印窗口显示在最前面,这里是在JDialog上画的表格
jdialog = new JDialog();
jdialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return headers.length; }
public int getRowCount() { return data.length;}
public Object getValueAt(int row, int col) {
return data[row][col];}
public String getColumnName(int column) {
return headers[column];}
public Class getColumnClass(int col) {
return getValueAt(0,col).getClass();}
public boolean isCellEditable(int row, int col) {
return (col==1);}
};
tableView = new JTable(dataModel);
JScrollPane scrollpane = new JScrollPane(tableView);
scrollpane.setPreferredSize(new Dimension(500, 80));
jdialog.getContentPane().setLayout(new BorderLayout());
jdialog.getContentPane().add(BorderLayout.CENTER,scrollpane);
jdialog.pack();
printButton= new JButton();
printButton.setText("打印");
jdialog.getContentPane().add(BorderLayout.SOUTH,printButton);
RepaintManager.currentManager(jdialog).setDoubleBufferingEnabled(false);
printButton.addActionListener(this);
jdialog.setBounds(300, 100, 650, 500);
jdialog.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== printButton)
{
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(TablePrint.this);
pj.printDialog();
try{
pj.print();
}
catch (Exception PrintException) {}
}
jdialog.dispose();
}
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 fontDesent=g2.getFontMetrics().getDescent();
//leave room for page number
double pageHeight = pageFormat.getImageableHeight()-fontHeight;
double pageWidth = pageFormat.getImageableWidth();
double tableWidth = (double)
tableView.getColumnModel().getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
}
double headerHeightOnPage= tableView.getTableHeader().getHeight()*scale;
double tableWidthOnPage=tableWidth*scale;
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);
if(pageIndex>=totalNumPages) {
return NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
//bottom center
g2.drawString("Page: "+(pageIndex+1), (int)pageWidth/2-35, (int)(pageHeight+fontHeight-fontDesent));
g2.translate(0f,headerHeightOnPage);
g2.translate(0f,-pageIndex*pageHeightForTable);
//If this piece of the table is smaller
//than the size available,
//clip to the appropriate bounds.
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));
}
//else clip to 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
return Printable.PAGE_EXISTS;
}
编程依旧博大精深,在此还是略窥皮毛
在这留个脚印,以后继续踩踩...
Printable接口由当前页面 painter 的
Pageable时,使用
PageFormat实例和实现此接口的实例构成的实例用于描述每个页面。调用实现
Printable的实例,以打印页面的图形。
在
PrinterJob上可以设置
Printable(..)。当客户端随后通过调用
PrinterJob.print(..)启动打印时,控制被传送到打印系统,直到所有的页面完成打印。这是通过在文档中所有页面打印完成前一直调用
Printable.print(..)来实现的。使用
Printable接口时,只要打印系统请求,该打印就将页面内容提交给图像。
Printable.print(..)的参数包括描述该页面可打印区域(计算适合该页面的内容所需要的区域)的
PageFormat和页面索引(它指定请求页面的从 0 开始的打印流索引)。
在这里我通过链表来获取表格内容,以实现动态改变要打印出来的表格的大小,而我在打印方法中是用二维数组来实现打印和定位的,
因此有了个蛋痛的将链表转化为一维数组,然后又将一维数组转化为二维数组的过程。
链表转化为一维数组可以直接用toarray()方法
而一维数组转化为二维数组,int n = 4;//列
int m = osf.getInfoStrings().length/n;//行
String[][] data1 = new String[m]
;
int num = -1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
num++;
data1[i][j] = (String)osf.getInfoStrings()[num];
}
}
重要的实现部分就是,画出表格然后打印
为了使打印窗口显示在最前面,这里是在JDialog上画的表格
jdialog = new JDialog();
jdialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return headers.length; }
public int getRowCount() { return data.length;}
public Object getValueAt(int row, int col) {
return data[row][col];}
public String getColumnName(int column) {
return headers[column];}
public Class getColumnClass(int col) {
return getValueAt(0,col).getClass();}
public boolean isCellEditable(int row, int col) {
return (col==1);}
};
tableView = new JTable(dataModel);
JScrollPane scrollpane = new JScrollPane(tableView);
scrollpane.setPreferredSize(new Dimension(500, 80));
jdialog.getContentPane().setLayout(new BorderLayout());
jdialog.getContentPane().add(BorderLayout.CENTER,scrollpane);
jdialog.pack();
printButton= new JButton();
printButton.setText("打印");
jdialog.getContentPane().add(BorderLayout.SOUTH,printButton);
RepaintManager.currentManager(jdialog).setDoubleBufferingEnabled(false);
printButton.addActionListener(this);
jdialog.setBounds(300, 100, 650, 500);
jdialog.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== printButton)
{
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(TablePrint.this);
pj.printDialog();
try{
pj.print();
}
catch (Exception PrintException) {}
}
jdialog.dispose();
}
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 fontDesent=g2.getFontMetrics().getDescent();
//leave room for page number
double pageHeight = pageFormat.getImageableHeight()-fontHeight;
double pageWidth = pageFormat.getImageableWidth();
double tableWidth = (double)
tableView.getColumnModel().getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
}
double headerHeightOnPage= tableView.getTableHeader().getHeight()*scale;
double tableWidthOnPage=tableWidth*scale;
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);
if(pageIndex>=totalNumPages) {
return NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
//bottom center
g2.drawString("Page: "+(pageIndex+1), (int)pageWidth/2-35, (int)(pageHeight+fontHeight-fontDesent));
g2.translate(0f,headerHeightOnPage);
g2.translate(0f,-pageIndex*pageHeightForTable);
//If this piece of the table is smaller
//than the size available,
//clip to the appropriate bounds.
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));
}
//else clip to 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
return Printable.PAGE_EXISTS;
}
编程依旧博大精深,在此还是略窥皮毛
在这留个脚印,以后继续踩踩...
相关文章推荐
- java 操作颜色选择器和打印机实现打印功能【代码片段】
- Java jacob调用打印机打印word文档
- Java如何调用TSC打印机进行打印详解
- java 指定打印机 进行打印
- java调用条码打印机打印标签完整例子(打印中文,打印二维码)
- Java实现POS打印机无驱打印(转)
- 必学Excel2013打印机巧之让表格居中打印
- JavaWeb开发,使用js调用打印机打印页面
- java调用打印机打印图片
- JAVA 指定打印机进行打印
- Java调用Acrobat Reader后台指定打印机打印PDF文件的方法
- Java调用打印机打印指定路径图片
- JAVA调用系统打印机打印字符串
- Java调用TSC条码打印机接口打印条码和二维码
- 用java判断打印机打印服务的能力
- 本地打印机获取以及文件打印 java
- 有关于用java来调用打印机进行打印时的设置问题
- 打印机打印带表格word报错、漏字现象
- java 指定打印机 进行打印
- java如何调用本地打印机进行图片打印