public int print(LineNumberPanel lineNumPanel, Graphics g, PageFormat pf, int pageIndex)
      throws PrinterException {
    // TODO: Because of banded printing the OS and printer driver may actually call this method
    // two or more times for the same page, the first time is to help the printer determine extents.
    // http://stackoverflow.com/questions/1943556/why-does-the-java-printables-print-method-get-called-multiple-times-with-the-sa
    // It may be possible to get away with generating an array of BufferedImage for each page when
    // pg.print(); is fired and just have this function look up the already rastered pages.
    // - Robert B. Colton
    int pageLines = (int) Math.floor(pf.getImageableHeight() / lineHeight);
    int pageCount = (int) Math.ceil((float)getLineCount() / (float)pageLines);
    if (pageIndex >= pageCount) return Printable.NO_SUCH_PAGE;

    Graphics2D graphics2D = (Graphics2D) g;
    graphics2D.translate (pf.getImageableX(), pf.getImageableY());

    Object map = Toolkit.getDefaultToolkit().getDesktopProperty(
        "awt.font.desktophints"); //$NON-NLS-1$
    if (map != null) {
      graphics2D.addRenderingHints((Map<?, ?>) map);
    }

    // Editors like Studio and Eclipse usually do not paint background colors to avoid wasting the
    // users ink.
    // Fill background
    //g.setColor(getBackground());
    //g.fillRect(0, 0, pf.getImageableWidth(), pf.getImageableHeight());

    // Draw each line
    int insetY = lineLeading + lineAscent;
    int lastLines = pageIndex * pageLines;
    int lineCount = getLineCount();

    if (lineNumPanel != null) {
      lineNumPanel.printLineNumbers(g,lastLines,Math.min(pageLines,lineCount - lastLines),
          lineNumPanel.getLineNumberWidth(lineCount));
      graphics2D.translate(lineNumPanel.getLineNumberWidth(lastLines + lineCount),0);
    }

    for (int lineNum = 0; lineNum < pageLines && lineNum + lastLines < lineCount; lineNum++) {
      drawLine(g, lineNum + lastLines, insetY + lineNum * lineHeight);
    }
    return Printable.PAGE_EXISTS;
  }