Ban*_*njo 13 java printing swing jtextpane graphics2d
我已经搜索了这个答案的高低,并且空白了.
我需要使用一个页脚打印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 ...
}
但是,在系统回调到print()方法之前,PageFormat不可用,并且当它到达print()方法时,似乎无法修改页脚,因为页眉和页脚都定义为方法调用中的"final":
public Printable getPrintable(final MessageFormat headerFormat,
final MessageFormat footerFormat)
当然有人找到了一种简单的方法来完成这个基本功能.
提前感谢任何可以提供帮助的人.
我不确定下面有关 JEditorPane/JTextPane 分页和打印的文章的链接是否适合您,但它们肯定为我做了很多:
| 归档时间: |
|
| 查看次数: |
1888 次 |
| 最近记录: |