Skip to content

Commit 40fd006

Browse files
committed
Enhance PageSize class to support for predefined and custom Paper Sizes
1 parent 335c14c commit 40fd006

File tree

1 file changed

+79
-26
lines changed

1 file changed

+79
-26
lines changed

java/src/org/openqa/selenium/print/PageSize.java

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,83 @@ public class PageSize {
2525
private final double height;
2626
private final double width;
2727

28-
public PageSize() {
29-
// Initialize with defaults. A4 paper size defaults in cms.
30-
this.height = 27.94;
31-
this.width = 21.59;
32-
}
33-
34-
public PageSize(double height, double width) {
35-
this.height = height;
36-
this.width = width;
37-
}
38-
39-
public double getHeight() {
40-
return height;
41-
}
42-
43-
public double getWidth() {
44-
return width;
45-
}
46-
47-
public Map<String, Object> toMap() {
48-
final Map<String, Object> options = new HashMap<>(7);
49-
options.put("height", getHeight());
50-
options.put("width", getWidth());
51-
52-
return options;
53-
}
28+
// Default Constructor (A4 by default)
29+
public PageSize() {
30+
this(PaperSize.A4); // Delegate to predefined size constructor
31+
}
32+
33+
// Custom Size Constructor
34+
public PageSize(double height, double width) {
35+
this.height = height;
36+
this.width = width;
37+
}
38+
39+
// Constructor for Predefined Sizes A4,A6,LEGAL,TABLOID using PaperSize Enum
40+
public PageSize(PaperSize paperSize) {
41+
this(paperSize.getHeight(), paperSize.getWidth()); // Delegate to custom size constructor
42+
}
43+
44+
// Factory Methods for Predefined Sizes
45+
public static PageSize A4() {
46+
return new PageSize(PaperSize.A4);
47+
}
48+
49+
public static PageSize A6() {
50+
return new PageSize(PaperSize.A6);
51+
}
52+
53+
public static PageSize LEGAL() {
54+
return new PageSize(PaperSize.LEGAL);
55+
}
56+
57+
public static PageSize TABLOID() {
58+
return new PageSize(PaperSize.TABLOID);
59+
}
60+
61+
// Getters for Height and Width
62+
public double getHeight() {
63+
return height;
64+
}
65+
66+
public double getWidth() {
67+
return width;
68+
}
69+
70+
// Convert to Map (for serialization or configuration)
71+
public Map<String, Object> toMap() {
72+
final Map<String, Object> options = new HashMap<>();
73+
options.put("height", getHeight());
74+
options.put("width", getWidth());
75+
return options;
76+
}
77+
78+
// Enum for Predefined Sizes
79+
public enum PaperSize {
80+
A4(27.94, 21.59),
81+
A6(14.8, 10.5),
82+
LEGAL(35.56, 21.59),
83+
TABLOID(43.18, 27.94);
84+
85+
private final double height;
86+
private final double width;
87+
88+
PaperSize(double height, double width) {
89+
this.height = height;
90+
this.width = width;
91+
}
92+
93+
public double getHeight() {
94+
return height;
95+
}
96+
97+
public double getWidth() {
98+
return width;
99+
}
100+
}
101+
102+
@Override
103+
public String toString() {
104+
return String.format("PageSize[height=%.2f, width=%.2f]", height, width);
105+
}
106+
54107
}

0 commit comments

Comments
 (0)