printing a StyledDocument
Hi,
I'm trying to print a StyledDocument from xml in a JTextPane. My document is
longer than a page.
- I use a Printable TextPane.
- I tried to make a Book, page per page
but it's almost impossible to 'break' a
StyledDocument into suitable pages.
- My strategy was to add one line after the
others and check if the size of my page
was still within the margin.
- Then I added my pages one by one to the
book
- But I didn't succeed to work with that
- I use a book to collect my pages.
StyledDocument.
Now, I'm thinking to make my pages directly from my xml file.
Is there a way to print my textpane automatically making directly the right
page format ?
[786 byte] By [
javanartz] at [2007-9-26 20:54:55]

Hello !
Try the following code for printing your StyledDocument.
Hope this helps ?
Greetings,
&y
...
private final int INCH = 72;
private final double A4_WIDTH = 8.27 * INCH;
private final double A4_HEIGHT = 5 * INCH;
...
public void doPrint() {
PrinterJob printJob = PrinterJob.getPrinterJob();
Paper paper = new Paper();
int margin = INCH/2;
double pageWidth = A4_WIDTH - 2 * margin;
double pageHeigth = A4_HEIGHT - 2 * margin;
paper.setImageableArea(margin, margin, pageWidth, pageHeigth) ;
PageFormat pformat = printJob.defaultPage();
pformat.setPaper(paper);
pformat.setOrientation(PageFormat.PORTRAIT);
printJob.pageDialog(pformat);
printJob.setPrintable(this);
if(printJob.printDialog()) {
try {
printJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
graphics.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());
int wPage = (int)pageFormat.getImageableWidth();
int hPage = (int)pageFormat.getImageableHeight();
graphics.setClip(0, 0, wPage, hPage);
if (printView == null) {
BasicTextUI btui = (BasicTextUI)myTextPane.getUI(); //your textpane
View root = btui.getRootView(textPane);
Element element = myStyledDocument().getDefaultRootElement(); //your styledDocument
printView = new PrintView(element, root, wPage, hPage);
}
boolean bContinue = printView.paintPage(graphics, hPage, pageIndex);
System.gc();
if (bContinue) {
return PAGE_EXISTS;
}
else {
printView = null;
return NO_SUCH_PAGE;
}
}
...
And you also need the following ;)
public class PrintView extends BoxView {
protected int firstOnPage = 0;
protected int lastOnPage = 0;
protected int pageIndex = 0;
/**
* ctor PrintView
*
* @param elem the rootelement of the JTextPane's DefaultStyledDocument
* @param root the rootView of the JTextPane that should be printed
* @param w the width
* @param h the height
*/
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 pIndex) {
if (pIndex > this.pageIndex) {
firstOnPage = lastOnPage + 1;
if (firstOnPage >= getViewCount()) {
return false;
}
pageIndex = pIndex;
}
int yMin = getOffset(Y_AXIS, firstOnPage);
int yMax = yMin + hPage;
Rectangle rc = new Rectangle();
for (int k = 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;
}
lastOnPage = k;
rc.y -= yMin;
paintChild(g, rc, k);
}
return true;
}
}