try {
long start = System.currentTimeMillis();
rootBox = BoxBuilder.createRootBox(layout, doc);
PageBox first = Layer.createPageBox(layout, "first");
rootBox.setContainingBlock(new ViewportBox(new Rectangle(0, 0,
first.getContentWidth(layout), first
.getContentHeight(layout))));
rootBox.layout(layout);
long end = System.currentTimeMillis();
XRLog.layout(Level.INFO, "Layout took " + (end - start) + "ms");
} catch (Throwable e) {
XRLog.exception(e.getMessage(), e);
return;
}
Layer root = rootBox.getLayer();
Dimension intrinsic_size = root.getPaintingDimension(layout);
root.trimEmptyPages(layout, intrinsic_size.height);
root.assignPagePaintingPositions(layout, Layer.PAGED_MODE_PRINT);
// RENDER
c = newRenderingContext(gc);
List pages = root.getPages();
c.setPageCount(pages.size());
if (startPage < 0) {
startPage = 0;
} else if (startPage >= pages.size()) {
startPage = pages.size() - 1;
}
if (endPage < 0 || endPage >= pages.size()) {
endPage = pages.size() - 1;
}
Shape working = c.getOutputDevice().getClip();
for (int i = startPage; i <= endPage; i++) {
PageBox page = (PageBox) pages.get(i);
c.setPage(i, page);
if (!_printer.startPage()) {
return;
}
page.paintBackground(c, 0, Layer.PAGED_MODE_PRINT);
page.paintMarginAreas(c, 0, Layer.PAGED_MODE_PRINT);
page.paintBorder(c, 0, Layer.PAGED_MODE_PRINT);
Rectangle content = page.getPrintClippingBounds(c);
c.getOutputDevice().clip(content);
int top = -page.getPaintingTop()
+ page.getMarginBorderPadding(c, CalculatedStyle.TOP);
int left = page.getMarginBorderPadding(c, CalculatedStyle.LEFT);
c.getOutputDevice().translate(left, top);
root.paint(c);
c.getOutputDevice().translate(-left, -top);