Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6560196

Printing of JEditorPane with HTML causes large spool file

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6
    • client-libs
    • 2d
    • x86
    • windows_2000

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 2000 Professional 5.0.2195 SP4

      A DESCRIPTION OF THE PROBLEM :
      When printing a JEditorPane (see example), a spool file is created, that is 5 times larger than the spool file, that is created with Java 1.4

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just print a JEditorPane with HTML input like in the example.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would expect a spool file that is not noticable larger than that in Java 1.4
      ACTUAL -
      I get a spool file 5 times larger than that in Java 1.4

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.print.PageFormat;
      import java.awt.print.Pageable;
      import java.awt.print.Printable;
      import java.awt.print.PrinterException;
      import java.awt.print.PrinterJob;

      import javax.swing.JButton;
      import javax.swing.JEditorPane;
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;


      /**
       * This is a class to show different memory usage between
       * Java 1.4 and 1.6. It creates a simple JEdiorPane with some
       * HTML input. The JEditorPane is printed 250 times (see
       * parameter PAGE_COUNT) to emphasize the memory usage.
       *
       * @author goessl
       */
      public class PrintableTest implements Printable, Pageable {

          private static final int PAGE_COUNT = 250;
          
          JFrame frame;
          JEditorPane pane;
          PrinterJob job;
          PageFormat format;

          double scaleX;
          double scaleY;
          
          private static String CELL = "<TD align=\"center\"><font style=\"font-size: 18; font-family:Times New Roman\">12:00</font></TD>";
          private static String TABLE_BEGIN = "<TABLE BORDER=1 bordercolordark=gray bordercolorlight=white cellpadding=1 cellspacing=0 width=100%>";
          private static String TABLE_END = "</TABLE>";
          
          /**
           * Initialize GUI components
           */
          private void init() {
              job = PrinterJob.getPrinterJob();
              format = job.defaultPage();

              frame = new JFrame("PrintableTest");
              frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              frame.getRootPane().setLayout(new BorderLayout());
              
              pane = new JEditorPane();
              pane.setPreferredSize(new Dimension(500, 650));
              pane.setEditable(false);
              pane.setContentType("text/html");
              StringBuffer buffer = new StringBuffer();
              buffer.append("<html><body>").append(TABLE_BEGIN);
              for (int i = 0; i < 20; i++) {
                  buffer.append("<tr>");
                  for (int j = 0; j < 10; j++) {
                      buffer.append(CELL);
                  }
                  buffer.append("</tr>");
              }
              buffer.append(TABLE_END).append("</body></html>");
              pane.setText(buffer.toString());
              
              JButton printButton = new JButton("Print it!");
              printButton.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      if (job.printDialog()) {
                          try {
                              scaleToFit();
                              job.setPageable(PrintableTest.this);
                              job.print();
                          } catch (PrinterException pe) {
                              // TODO Auto-generated catch block
                              pe.printStackTrace();
                          }
                      }
                  }
                  
              });
              
              frame.getRootPane().add(new JScrollPane(pane), BorderLayout.CENTER);
              frame.getRootPane().add(printButton, BorderLayout.SOUTH);
          }

          /**
           * Show the test frame
           */
          private void show() {
              frame.pack();
              frame.show();
          }
          
          private void scaleToFit() {
              scaleX = format.getImageableWidth() / pane.getWidth();
              scaleY = format.getImageableHeight() / pane.getHeight();
              if (scaleX > scaleY) {
                  scaleX = scaleY;
              } else {
                  scaleY = scaleX;
              }
          }

          /**
           * @see java.awt.print.Pageable#getNumberOfPages()
           */
          public int getNumberOfPages() {
              return PAGE_COUNT;
          }

          /**
           * @see java.awt.print.Pageable#getPageFormat(int)
           */
          public PageFormat getPageFormat(int pageIndex) {
              return format;
          }

          /**
           * @see java.awt.print.Pageable#getPrintable(int)
           */
          public Printable getPrintable(int pageIndex) {
              return this;
          }

          /**
           * Main method
           *
           * @param args
           */
          public static void main(String[] args) {
              PrintableTest test = new PrintableTest();
              test.init();
              test.show();
          }

          /**
           * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
           */
          public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
              if (pageIndex < PAGE_COUNT) {
                  Graphics2D g2 = (Graphics2D) graphics;
                  g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                  g2.scale(scaleX, scaleY);
                  pane.paint(g2);
                  return PAGE_EXISTS;
              } else {
                  return NO_SUCH_PAGE;
              }
          }

      }

      ---------- END SOURCE ----------

            prr Philip Race
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: