在JTable之前和之后附加要打印的文本 [英] Append text for printing before and after a JTable
问题描述
我正在尝试打印 JTable
而 print()
方法效果很好,直到我来到这个场景。让我说我想打印之前,只在第一页(不是标题)文本报告,最后文本这是报告的结束。我想再一次澄清我不需要页眉或页脚,只有当我打印它们时,这个文本出现在最后一页的第一页和最后一页的顶部。
I am trying to print a JTable
and the print()
method works great till I come to this scenario. Lets say I want to print before, in the first page only (not header) the text "Report" and on the end the text "This is the end of report". I would like once more to clarify that I don't need a header or footer only this text to appear in the top of the first and bottom of the last page when I print them.
我该怎么做?
推荐答案
一种方法是追加( )
一系列合适的 Printable
实例到 java.awt.print.Book
,如图所示这里。
One way to do this is to append()
a series of suitable Printable
instances to a java.awt.print.Book
, as shown here.
附录: JTable
有 getPrintable()
应简化事物的方法;这是一个大纲和简单标题可打印
:
PrinterJob pj = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new Title(), pj.defaultPage());
book.append(table.getPrintable(...), pj.defaultPage());
book.append(new EndPage(), pj.defaultPage());
pj.setPageable(book);
pj.print();
...
private static class Title implements Printable {
Font font = new Font("SansSerif", Font.PLAIN, 48);
@Override
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.setFont(font);
g2d.setColor(Color.black);
g2d.drawString("Report", 50, 200);
return Printable.PAGE_EXISTS;
}
}
这篇关于在JTable之前和之后附加要打印的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!