我已经搜索了这个答案的高低,并且空白了.
我需要使用一个页脚打印JTextPane的内容,该页脚显示"<m>页面的页面<n>".在Java中似乎不可能做这个简单的功能.
当我得到Printable时,我可以设置页脚打印页码,例如
String header = "whatever";
String footer = " Page - {0}";
printText(textPane.getPrintable(new MessageFormat(header),
new MessageFormat(footer)));
但是,在调度打印机对话框之前,似乎无法找到将要打印的总页数.我认为这是因为该对话框用于在页面发送到打印机之前格式化页面.打印机对话框始终显示只有一(1)页.
所以,我开始编写一个例程,它将遍历JTextPane文档并通过从print()方法中的PageFormat获取可视区域来计算页面,然后使用每行的高度(fontsize)来计算每行中的行数.页面,然后计算页数.eg
int maxh = (int) pf.getImageableHeight ();
Element section = doc.getDefaultRootElement();
for (int i=0; i<paraCount; i++)
{
Element e = section.getElement(i);
// Get the attributeSet for this paragraph (Element)
// The attributeSet contains the Styles, which contain
// the fonts etc.
AttributeSet attrs = e.getAttributes();
int fontsize = StyleConstants.getFontSize(attrs);
// .... add up the lines and count filled pages ...
} …