Overview
The PageSize class defines the height and width dimensions for a printed page, with predefined constants for common paper sizes such as ISO A4, US Letter, US Legal, and ANSI Tabloid.
Description
PageSize is an immutable value object representing the physical dimensions of a printed page in centimeters. It provides predefined static constants for standard paper sizes and a default constructor that uses ISO A4 dimensions. The class includes a factory method setPageSize() for creating copies, a toMap() method for wire protocol serialization, and a toString() override for debugging.
Usage
Use PageSize when configuring the output page dimensions for the PrintOptions class. Select a predefined constant for standard paper sizes or create a custom size with specific height and width values in centimeters.
Code Reference
Source Location
Signature
public class PageSize {
public static final PageSize ISO_A4;
public static final PageSize US_LEGAL;
public static final PageSize ANSI_TABLOID;
public static final PageSize US_LETTER;
public PageSize();
public PageSize(double height, double width);
public double getHeight();
public double getWidth();
public static PageSize setPageSize(PageSize pageSize);
public Map<String, Object> toMap();
public String toString();
}
Import
import org.openqa.selenium.print.PageSize;
I/O Contract
Predefined Constants
| Constant |
Height (cm) |
Width (cm) |
Description
|
ISO_A4 |
29.7 |
21.0 |
ISO A4 paper size
|
US_LEGAL |
35.56 |
21.59 |
US Legal paper size
|
ANSI_TABLOID |
43.18 |
27.94 |
ANSI Tabloid paper size
|
US_LETTER |
27.94 |
21.59 |
US Letter paper size
|
Constructors
| Constructor |
Parameters |
Description
|
PageSize() |
none |
Creates a page size with ISO A4 defaults (29.7 x 21.0 cm)
|
PageSize(double, double) |
height, width |
Creates a page size with custom dimensions in centimeters
|
Methods
| Method |
Parameters |
Returns |
Description
|
getHeight() |
none |
double |
Returns the page height in centimeters
|
getWidth() |
none |
double |
Returns the page width in centimeters
|
setPageSize (static) |
PageSize pageSize |
PageSize |
Creates a new PageSize copy from an existing one; throws IllegalArgumentException if null
|
toMap() |
none |
Map<String, Object> |
Serializes to a map with "height" and "width" keys
|
Serialization (toMap)
| Map Key |
Type |
Description
|
height |
double |
Page height in centimeters
|
width |
double |
Page width in centimeters
|
Usage Examples
// Use the default A4 page size
PageSize defaultSize = new PageSize(); // 29.7 x 21.0 cm
// Use a predefined constant
PageSize letterSize = PageSize.US_LETTER; // 27.94 x 21.59 cm
PageSize legalSize = PageSize.US_LEGAL; // 35.56 x 21.59 cm
PageSize tabloidSize = PageSize.ANSI_TABLOID; // 43.18 x 27.94 cm
// Create a custom page size (e.g., 20 x 15 cm)
PageSize customSize = new PageSize(20.0, 15.0);
// Apply to print options
PrintOptions printOptions = new PrintOptions();
printOptions.setPageSize(letterSize);
// Serialize to wire format
Map<String, Object> sizeMap = letterSize.toMap();
// Result: {height=27.94, width=21.59}
// String representation
System.out.println(letterSize);
// Output: PageSize[width=21.59, height=27.94]
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.