JDK-4886069 : Printing: Legal page sizes not correctly selected/printed on Linux
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 1.4.1,1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux,solaris_8
  • CPU: x86,sparc
  • Submitted: 2003-07-02
  • Updated: 2004-02-16
  • Resolved: 2003-09-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.2_04 04Fixed
Description

Name: jk109818			Date: 07/02/2003


FULL PRODUCT VERSION :
java version "1.4.2-beta"
Java(TM) 2 Runtime Envinronment Standard Edition (build 1.4.2-beta-b19)
Java HotSpot(TM) Client VM (build 1.4.2-beta-b19, mixed mode)

Also tested under 1.4.1_02 build 1.4.1_02-b06


FULL OS VERSION :
Linux 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2001 i686 unknown

This bug DOES NOT exist in Windows versions of Java, just Linux.

EXTRA RELEVANT SYSTEM CONFIGURATION :
Printer Tested: Lexmark Optra E310


A DESCRIPTION OF THE PROBLEM :
Cannot print to legal size paper and get correct output.

We are currently trying to use the new Printing API of ver 1.4.x to print to legal (8.5" x 14") paper. When legal paper is selected from the Print Dialog the correct print area size is returned to the java program but the output from the printer seems to be shifted up about 3".



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1- Run the included sample program.
2- Choose the "Print" menu option.
3- Go to the "Page Setup" tab
4- Choose "Legal" from the "Size" combobox.
5- Click Print

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The sample program contains a JTextArea that is set to be approximately the size of the printable area on a legal sheet of paper with 1" margins.  So we expect that when the print menu option is clicked and legal paper is selected that this text area (with all 27 numbered lines) print on the legal page with 1" margins all around.
ACTUAL -
When the above steps are followed, the printed area seems to be shifted up the page about 3 inches so that at the top of the page there is no 1" margin and the text starts at line #6.  At the bottom there is a 4" margin instead of just a 1" margin.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import java.awt.print.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import java.io.*;


public class PrintTest extends JFrame {
  private JPanel contentPane;
  private JMenuBar jMenuBar1 = new JMenuBar();
  private JMenu jMenuFile = new JMenu();
  private JMenuItem jMenuItem1 = new JMenuItem();
  private BorderLayout borderLayout1 = new BorderLayout();
  private JPanel jPanel1 = new JPanel();
  private BorderLayout borderLayout2 = new BorderLayout();
  private JScrollPane jScrollPane1 = new JScrollPane();
  private JTextArea jTextArea1 = new JTextArea();
  private Border border1;

  //Construct the frame
  public PrintTest() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();

    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    border1 = BorderFactory.createLineBorder(Color.black,1);
    contentPane.setLayout(borderLayout1);
    this.setTitle("Print Test");
    jMenuFile.setText("File");
    jMenuItem1.setText("Print");
    jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(80, java.awt.event.KeyEvent.CTRL_MASK, false));
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItem1_actionPerformed(e);
      }
    });
    jPanel1.setLayout(borderLayout2);
    jTextArea1.setBorder(border1);
    jTextArea1.setText("1. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "2. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "3. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "4. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "5. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "6. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "7. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "8. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "9. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "10. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "11. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "12. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "13. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "14. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "15. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "16. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "17. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "18. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "19. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "20. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "21. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "22. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "23. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "24. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "25. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "26. This is a printer test designed to illustrate a bug in the java printing API.\n\n"+
                       "27. This is a printer test designed to illustrate a bug in the java printing API.");
    jMenuFile.add(jMenuItem1);
    contentPane.add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jScrollPane1, BorderLayout.CENTER);
    jScrollPane1.getViewport().add(jTextArea1, null);
    jScrollPane1.setPreferredSize(new Dimension(468,648));
    jTextArea1.setPreferredSize(new Dimension(468,864));
    jMenuBar1.add(jMenuFile);
    this.setJMenuBar(jMenuBar1);
  }

  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  void jMenuItem1_actionPerformed(ActionEvent e) {
    PrintUtils.printComponent(jTextArea1);
  }




  public static class PrintUtils implements Printable {
    private JComponent componentToBePrinted;
    protected double scale =1.0;
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();


    public static void printComponent(JComponent c) {
      new PrintUtils(c).print();
    }

    public PrintUtils(JComponent componentToBePrinted) {
      this.componentToBePrinted = componentToBePrinted;

    }

    void print() {
      DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;

      PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor,pras);
      PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

      PrintService service = ServiceUI.printDialog(null,100,100,printService,defaultService,flavor,pras);

      if(service != null) {
        DocPrintJob job = service.createPrintJob();
        DocAttributeSet das = new HashDocAttributeSet();

        Doc doc = new SimpleDoc(this,flavor,das);

        try {
          job.print(doc,pras);

        } catch(PrintException pe) {
          pe.printStackTrace();
        }
      }

    }


    public int print(Graphics g, PageFormat pageFormat, int pageIndex)
    {

      double h=componentToBePrinted.getHeight();
      double pageHeight=pageFormat.getImageableHeight();

      if (pageIndex * pageHeight > h * scale) {
        return(NO_SUCH_PAGE);
      } else {

        Graphics2D g2d = (Graphics2D)g;

        //move past unprintable area
        double xOffset=pageFormat.getImageableX();
        double yOffset=pageFormat.getImageableY();
        g2d.translate(xOffset,yOffset);


        //move to correct page taking into account the scaling
        double newx=0;
        double newy=pageHeight*(-pageIndex);
        g2d.translate(newx / 1.0,newy / 1.0 );

        //print

        componentToBePrinted.print(g2d);
        return(PAGE_EXISTS);
        }
    }

    public static void disableDoubleBuffering(Component c) {
      RepaintManager currentManager = RepaintManager.currentManager(c);
      currentManager.setDoubleBufferingEnabled(false);
    }

    /** Re-enables double buffering globally. */

    public static void enableDoubleBuffering(Component c) {
      RepaintManager currentManager = RepaintManager.currentManager(c);
      currentManager.setDoubleBufferingEnabled(true);
    }
}

  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    PrintTest frame = new PrintTest();
    frame.pack();

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.setVisible(true);
  }

} 
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
We did a little bit of investigating into the Postscript file that is created by the Print routine if you print to a file.  It seems like the postscript file is missing some of the information that it needs to tell the printer to print on legal paper the: "<< /PageSize [612 1008] /ManualFeed true >> setpagedevice" tag is one that is missing but there may be others.  If you manually edit the ps file to have these correct tags then the page is printed correctly.
(Review ID: 188770) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.4.2_04 generic tiger FIXED IN: 1.4.2_04 tiger INTEGRATED IN: 1.4.2_04 tiger tiger-b22 VERIFIED IN: 1.4.2_04
14-06-2004

EVALUATION Per description, there are missing postscript commands for selecting page size. ###@###.### 2003-09-15
15-09-2003