In the development of my application I need to print the contents of a
JTextPane. Everything is ok with this, except that it only prints the first
page. Does anybody know what causes this, because i'm out of ideas.
Here's some sample code for the printing :
PrinterJob prnJob = PrinterJob.getPrinterJob();
prnJob.setPrintable(TextViewer.this);
prnJob.print();
public int print(Graphics pg, PageFormat pageFormat,int pageIndex) throws
PrinterException {
pg.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());
int wPage = (int)pageFormat.getImageableWidth();
int hPage = (int)pageFormat.getImageableHeight();
pg.setClip(0, 0, wPage, hPage);
// Only do this once per print
if (m_printView == null) {
BasicTextUI btui = (BasicTextUI)textpane.getUI();
View root = btui.getRootView(textpane);
m_printView = new PrintView(m_doc.getDefaultRootElement(), root,
wPage, hPage);
}
boolean bContinue = m_printView.paintPage(pg, hPage, pageIndex);
System.gc();
if (bContinue)return PAGE_EXISTS;
else {
m_printView = null;
return NO_SUCH_PAGE;
}
}
class PrintView extends BoxView
{
protected int m_firstOnPage = 0;
protected int m_lastOnPage = 0;
protected int m_pageIndex = 0;
public PrintView(Element elem, View root, int w, int h) {
super(elem, Y_AXIS);
setParent(root);
setSize(w, h);
layout(w, h);
}
public boolean paintPage(Graphics g, int hPage, int pageIndex) {
if (pageIndex > m_pageIndex) {
m_firstOnPage = m_lastOnPage + 1;
if (m_firstOnPage >= getViewCount()) return false;
m_pageIndex = pageIndex;
}
int yMin = getOffset(Y_AXIS, m_firstOnPage);
int yMax = yMin + hPage;
Rectangle rc = new Rectangle();
for (int k = m_firstOnPage; k < getViewCount(); k++) {
rc.x = getOffset(X_AXIS, k);
rc.y = getOffset(Y_AXIS, k);
rc.width = getSpan(X_AXIS, k);
rc.height = getSpan(Y_AXIS, k);
if (rc.y+rc.height > yMax)
break;
m_lastOnPage = k;
rc.y -= yMin;
paintChild(g, rc, k);
}
return true;
}
}
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".