Shape working = g.getClip();
List pages = root.getPages();
c.setPageCount(pages.size());
for (int i = 0; i < pages.size(); i++) {
PageBox page = (PageBox)pages.get(i);
c.setPage(i, page);
g.setClip(working);
Rectangle overall = page.getScreenPaintingBounds(c, pagePaintingClearanceWidth);
overall.x -= 1;
overall.y -= 1;
overall.width += 1;
overall.height += 1;
Rectangle bounds = new Rectangle(overall);
bounds.width += 1;
bounds.height += 1;
if (working.intersects(bounds)) {
page.paintBackground(c, pagePaintingClearanceWidth, Layer.PAGED_MODE_SCREEN);
page.paintMarginAreas(c, pagePaintingClearanceWidth, Layer.PAGED_MODE_SCREEN);
page.paintBorder(c, pagePaintingClearanceWidth, Layer.PAGED_MODE_SCREEN);
Color old = g.getColor();
g.setColor(Color.BLACK);
g.drawRect(overall.x, overall.y, overall.width, overall.height);
g.setColor(old);
Rectangle content = page.getPagedViewClippingBounds(c, pagePaintingClearanceWidth);
g.clip(content);
int left = pagePaintingClearanceWidth +
page.getMarginBorderPadding(c, CalculatedStyle.LEFT);
int top = page.getPaintingTop()
+ page.getMarginBorderPadding(c, CalculatedStyle.TOP)
- page.getTop();
g.translate(left, top);
root.paint(c);
g.translate(-left, -top);