-
Bug
-
Resolution: Not an Issue
-
P3
-
None
-
5.0
-
x86
-
windows_xp
FULL PRODUCT VERSION :
java version "1.5.0_09"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_09-b03)
Java HotSpot(TM) Client VM (build 1.5.0_09-b03, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
The method java.awt.print.PageFormat.setPaper(Paper) does no longer work in Java 5. The default paper size will instead be used.
(We need to set the paper size for a printer from the java program.)
It "setPaper(..)" works perfect in Java 1.4 (Java HotSpot(TM) Client VM (build 1.4.2_07-b05, mixed mode), but no longer in the latest Java 5. The default page size is taken instead. Hence text beyond this size will NOT be written on the paper.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.
Create a program that sets the width according to the description.
2.
Install a printer that you manually must set the paper size in the printJob.printDialog().
Note: You can also use the "Microsoft Officer Image Document Image Writer" (i.e. a "pdf" writer) as well
3.
Print one page.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It should be possible to write text everywhere within the specified size.
ACTUAL -
The default page size is used. Text beyond this size is not written to the paper.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
// Search on the string "THE PROBLEM:" to get the problem line
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SetPageSizeAndPrint extends JPanel implements Printable, ActionListener {
final static Color bg = Color.white;
final static Color fg = Color.black;
final static JButton button = new JButton("Print");
final static String[] text = {"This is a test text.",
"abcdefghijklmnopqrstuvwxyz1234567890",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ!\"§$%&/()=",
"öäüÖÄÜ .,-"};
public SetPageSizeAndPrint() {
setBackground(bg);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
PrinterJob printJob = PrinterJob.getPrinterJob();
//PageFormat pageformat = printJob.pageDialog(printJob.defaultPage());
if (printJob.printDialog()) {
try {
PageFormat pageformat = printJob.defaultPage();
Paper paper = pageformat.getPaper();
int x = 72;
int y = 72;
int width = 853;
int height = 697;
System.out.println("Area: X="+x+", Y="+y+", W="+width+", H="+height);
paper.setImageableArea(x, y, width, height);
System.out.println("Paper: W="+paper.getWidth()+", H="+paper.getHeight());
paper.setSize(width, height); // THE PROBLEM: WORKS IN JAVA 4 BUT NOT IN JAVA 5!
pageformat.setPaper(paper);
printJob.setPrintable(this, pageformat);
printJob.print();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawShapes(g2);
}
public void drawShapes(Graphics2D g2){
int i = 0;
FontMetrics fontmetrics = g2.getFontMetrics();
String font = "Courier";
int size=12;
g2.setFont(new Font(font, Font.PLAIN, size));
for (int a = 0; a < text.length; a++) {
g2.drawString(text[a], 0, i + fontmetrics.getAscent());
//i += 12;
i += fontmetrics.getHeight();
}
}
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
System.out.println("X=" + pf.getImageableX());
System.out.println("Y=" + pf.getImageableY());
System.out.println("W=" + pf.getImageableWidth());
System.out.println("H=" + pf.getImageableHeight());
System.out.println("PW = " + pf.getPaper().getWidth());
System.out.println("PH = " + pf.getPaper().getHeight());
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}
g2.translate(pf.getImageableX(), pf.getImageableY());
drawShapes(g2);
g2.draw(new Rectangle(0, 0, (int) pf.getImageableWidth(), (int) pf.getImageableHeight()));
return Printable.PAGE_EXISTS;
}
public static void main(String s[]){
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowClosed(WindowEvent e) {System.exit(0);}
};
JFrame f = new JFrame();
f.addWindowListener(l);
JPanel panel = new JPanel();
panel.add(button);
f.setTitle("Printing Test");
f.getContentPane().add(BorderLayout.SOUTH, panel);
f.getContentPane().add(BorderLayout.CENTER, new SetPageSizeAndPrint());
f.setSize(580, 500);
f.setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
I have not found any.
java version "1.5.0_09"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_09-b03)
Java HotSpot(TM) Client VM (build 1.5.0_09-b03, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
The method java.awt.print.PageFormat.setPaper(Paper) does no longer work in Java 5. The default paper size will instead be used.
(We need to set the paper size for a printer from the java program.)
It "setPaper(..)" works perfect in Java 1.4 (Java HotSpot(TM) Client VM (build 1.4.2_07-b05, mixed mode), but no longer in the latest Java 5. The default page size is taken instead. Hence text beyond this size will NOT be written on the paper.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.
Create a program that sets the width according to the description.
2.
Install a printer that you manually must set the paper size in the printJob.printDialog().
Note: You can also use the "Microsoft Officer Image Document Image Writer" (i.e. a "pdf" writer) as well
3.
Print one page.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It should be possible to write text everywhere within the specified size.
ACTUAL -
The default page size is used. Text beyond this size is not written to the paper.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
// Search on the string "THE PROBLEM:" to get the problem line
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SetPageSizeAndPrint extends JPanel implements Printable, ActionListener {
final static Color bg = Color.white;
final static Color fg = Color.black;
final static JButton button = new JButton("Print");
final static String[] text = {"This is a test text.",
"abcdefghijklmnopqrstuvwxyz1234567890",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ!\"§$%&/()=",
"öäüÖÄÜ .,-"};
public SetPageSizeAndPrint() {
setBackground(bg);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
PrinterJob printJob = PrinterJob.getPrinterJob();
//PageFormat pageformat = printJob.pageDialog(printJob.defaultPage());
if (printJob.printDialog()) {
try {
PageFormat pageformat = printJob.defaultPage();
Paper paper = pageformat.getPaper();
int x = 72;
int y = 72;
int width = 853;
int height = 697;
System.out.println("Area: X="+x+", Y="+y+", W="+width+", H="+height);
paper.setImageableArea(x, y, width, height);
System.out.println("Paper: W="+paper.getWidth()+", H="+paper.getHeight());
paper.setSize(width, height); // THE PROBLEM: WORKS IN JAVA 4 BUT NOT IN JAVA 5!
pageformat.setPaper(paper);
printJob.setPrintable(this, pageformat);
printJob.print();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawShapes(g2);
}
public void drawShapes(Graphics2D g2){
int i = 0;
FontMetrics fontmetrics = g2.getFontMetrics();
String font = "Courier";
int size=12;
g2.setFont(new Font(font, Font.PLAIN, size));
for (int a = 0; a < text.length; a++) {
g2.drawString(text[a], 0, i + fontmetrics.getAscent());
//i += 12;
i += fontmetrics.getHeight();
}
}
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
System.out.println("X=" + pf.getImageableX());
System.out.println("Y=" + pf.getImageableY());
System.out.println("W=" + pf.getImageableWidth());
System.out.println("H=" + pf.getImageableHeight());
System.out.println("PW = " + pf.getPaper().getWidth());
System.out.println("PH = " + pf.getPaper().getHeight());
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}
g2.translate(pf.getImageableX(), pf.getImageableY());
drawShapes(g2);
g2.draw(new Rectangle(0, 0, (int) pf.getImageableWidth(), (int) pf.getImageableHeight()));
return Printable.PAGE_EXISTS;
}
public static void main(String s[]){
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowClosed(WindowEvent e) {System.exit(0);}
};
JFrame f = new JFrame();
f.addWindowListener(l);
JPanel panel = new JPanel();
panel.add(button);
f.setTitle("Printing Test");
f.getContentPane().add(BorderLayout.SOUTH, panel);
f.getContentPane().add(BorderLayout.CENTER, new SetPageSizeAndPrint());
f.setSize(580, 500);
f.setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
I have not found any.