JDK-6560196 : Printing of JEditorPane with HTML causes large spool file
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2007-05-21
  • Updated: 2010-04-04
  • Resolved: 2007-05-22
Related Reports
Duplicate :  
Description
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 ----------

Comments
EVALUATION Yes, its a dup. JDK 7 b03 reverts the spool file to almost the same size as 1.4.2
22-05-2007

EVALUATION Since a physical font is specified and this is a Swing text component being printed on windows, I suspect its the same as : 6452415:Swing text components using a physical font print text as filled shapes (bigger spool files). This is already fixed in JDK7, so I can test against that. As previously noted, it probably should be backported to 6ux (6u3?)
21-05-2007