我想打印2个不同字体的标题,然后打印一个表格的标题,所有行都用另一种字体打印。我在JTable
中有表格内容。当用户点击按钮时,我想将所有内容发送到PrinterServices
并以LANDSCAPE
方向打印。可能有很多行,所以我需要进行分页。分页在左下角。
我发现无法使用JTable.print
,因为我需要font1
中的标题,font2
中的副标题,以及font3
中的表格标题/表格数据1}}。
我该怎么做?
我看过这个页面,向我展示了如何在几个页面中以不同的风格进行打印:
http://www.java2s.com/Code/Java/2D-Graphics-GUI/PrintinJavapageformatanddocument.htm
但我认为此链接会将不同的样式放在不同的页面中,在我的情况下,title1
和title2
位于第一页,而页面的其余部分则填充了行数据,然后第2页,第3页......因此它在同一页面中包含多个样式,并不完全是链接的样式。
编辑:
现在我有了这些代码,但它们并没有在两个方面发挥作用:
我必须在打印对话框提示中设置方向(选择"水平"方向)。我默认需要它,但我不想要Java本机页面设置对话框。我希望系统依赖一个。
左边距/缩进随页码增加而增加。我希望它是静态的。我一定搞砸了坐标。
分页脚不存在。我怎么能得到它?
所以我有:
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
public class PrintingTest1 {
/**
* @param args
*/
public static void main(String[] args) {
PrinterJob job = PrinterJob.getPrinterJob();
//cross-platform print dialog, java l&f
// PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
//java page setup dialog
// PageFormat pf = job.getPageFormat(aset);
// job.setPrintable(new PrintingTest1().new HelloWorldPrinter(), pf);
//java print setup dialog
// boolean doPrint = job.printDialog(aset);
// if (doPrint) {
// try {
// job.print(aset);
// } catch (PrinterException e) {
// System.out.println("Cancelled. ");
// }
// }
//native print dialog
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
Font font1 = new Font("Courier New", Font.PLAIN, 12);
Font font2 = new Font("Courier New", Font.PLAIN, 10);
Font font3 = new Font("Courier New", Font.PLAIN, 9);
HelloWorldPrinter printer = new PrintingTest1().new HelloWorldPrinter();
printer.setFont1(font1);
printer.setFont2(font2);
printer.setFont3(font3);
printer.setTitle1("Title 1: Main title");
printer.setTitle2line1("Title 2 line 1: top");
printer.setTitle2line2("Title 2 line 2: bot");
printer.setHeaderline1("HeaderLine1: top");
printer.setHeaderline2("HeaderLine2: bot");
String[][] rows = new String[85][2];
for (int i=0; i<rows.length; i++) {
rows[i] = new String[] {"row " + i + ", line 1", " row " + i + ", line 2"};
}
printer.setRows(rows.length);
printer.setRowsData(rows);
job.setPrintable(printer);
boolean doPrint = job.printDialog();
if (doPrint) {
try {
job.print();
} catch (PrinterException e) {
System.out.println("Cancelled. ");
}
}
}
class HelloWorldPrinter implements Printable {
private Font font1;
private Font font2;
private Font font3;
private int lineHeight1;
private int lineHeight2;
private int lineHeight3;
private String title1;
private String title2line1;
private String title2line2;
private String headerline1;
private String headerline2;
private String[][] rowsData;
/**
* Number of rows in the table
*/
private int rows;
/**
* Number of lines of data to print. 2*rows.
*/
private int dataLines;
private int pageLimit;
private int linesDataOnPage0;
private int linesOnPage0;
private int linesPerPage;
/**
* The array index of row data when every new pages starts to print.
*/
private int newStartIndex;
public void setRows(int rows) {
this.rows = rows;
}
public void setFont1(Font font1) {
this.font1 = font1;
}
public void setFont2(Font font2) {
this.font2 = font2;
}
public void setFont3(Font font3) {
this.font3 = font3;
}
public void setTitle1(String title1) {
this.title1 = title1;
}
public void setTitle2line1(String title2line1) {
this.title2line1 = title2line1;
}
public void setTitle2line2(String title2line2) {
this.title2line2 = title2line2;
}
public void setHeaderline1(String headerline1) {
this.headerline1 = headerline1;
}
public void setHeaderline2(String headerline2) {
this.headerline2 = headerline2;
}
public void setRowsData(String[][] rowsData) {
this.rowsData = rowsData;
}
public HelloWorldPrinter() {
// TODO Auto-generated constructor stub
}
private int calculatePageLimit(int dataLines, Graphics g, PageFormat pf) {
FontMetrics metrics1 = g.getFontMetrics(font1);
FontMetrics metrics2 = g.getFontMetrics(font2);
FontMetrics metrics3 = g.getFontMetrics(font3);
lineHeight1 = metrics1.getHeight();
lineHeight2 = metrics2.getHeight();
lineHeight3 = metrics3.getHeight();
double pageHeight = pf.getImageableHeight();
//title 1, title 2 (2 lines), header (2 lines)
linesDataOnPage0 = (int)((pageHeight - lineHeight1 - lineHeight2*2 - lineHeight3*2) / lineHeight3);
linesOnPage0 = linesDataOnPage0 + 5;
linesPerPage = (int)pageHeight / lineHeight3;
int pageLimit0 = 0;
if (dataLines <= linesDataOnPage0) {
pageLimit0 = 1; //all data can be printed on the first page.
} else {
int lastPageLines = (dataLines - linesDataOnPage0) % linesPerPage;
pageLimit0 = (dataLines - linesDataOnPage0 - lastPageLines) / linesPerPage + 2;
}
return pageLimit0;
}
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageLimit == 0) { //not calculated yet.
this.dataLines = rows * 2;
pageLimit = calculatePageLimit(dataLines, graphics, pageFormat);
}
Paper papel = new Paper();
papel.setImageableArea(72, 72, 697, 450);
papel.setSize(841, 595);
pageFormat.setPaper(papel);
pageFormat.setOrientation(PageFormat.LANDSCAPE);
Graphics2D g2d = (Graphics2D)graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
if (pageIndex < pageLimit) {
int y = 10;
if (pageIndex == 0) {
// 1. print the title 1
g2d.setFont(font1);
g2d.drawString(title1, 10, y);
y += lineHeight1;
// 2. print title 2
g2d.setFont(font2);
g2d.drawString(title2line1, 10, y);
y += lineHeight2;
g2d.drawString(title2line2, 10, y);
y += lineHeight2;
// 3. print header
g2d.setFont(font3);
g2d.drawString(headerline1, 10, y);
y += lineHeight3;
g2d.drawString(headerline2, 10, y);
y += lineHeight3;
// 4. print data
for (int i=0; i< (int)(linesDataOnPage0 / 2); i++) {
g2d.drawString(rowsData[i][0], 10, y);
y += lineHeight3;
g2d.drawString(rowsData[i][1], 10, y);
y += lineHeight3;
}
y = 0;
newStartIndex = (int)(linesDataOnPage0 / 2);
return PAGE_EXISTS;
} else {
int end;
if (newStartIndex + (int)(linesPerPage / 2) > rowsData.length) {
end = rowsData.length;
} else {
end = newStartIndex + (int)(linesPerPage / 2);
}
g2d.setFont(font3);
for (int i=newStartIndex; i< end; i++) {
g2d.drawString(rowsData[i][0], 10, y);
y += lineHeight3;
g2d.drawString(rowsData[i][1], 10, y);
y += lineHeight3;
}
y = 0;
newStartIndex +=(int)(linesPerPage / 2);
return PAGE_EXISTS;
}
} else {
return NO_SUCH_PAGE;
}
}
}
}