Overview
The PageMargin class defines the top, bottom, left, and right margins for a printed page in the Selenium Print API.
Description
PageMargin is an immutable value object that represents the four margins of a printed page in centimeters. It provides a default constructor that sets all margins to 1.0 cm, and a parameterized constructor that accepts custom margin values. The class includes a toMap() method for serialization to the W3C WebDriver print command wire format.
Usage
Use PageMargin when configuring page margins for the PrintOptions class. Create a custom margin object and pass it to PrintOptions.setPageMargin() to control the spacing around printed page content.
Code Reference
Source Location
Signature
public class PageMargin {
public PageMargin();
public PageMargin(double top, double bottom, double left, double right);
public double getTop();
public double getBottom();
public double getLeft();
public double getRight();
public Map<String, Object> toMap();
}
Import
import org.openqa.selenium.print.PageMargin;
I/O Contract
Constructors
| Constructor |
Parameters |
Description
|
PageMargin() |
none |
Creates a margin with all sides set to 1.0 cm (default)
|
PageMargin(double, double, double, double) |
top, bottom, left, right |
Creates a margin with custom values in centimeters
|
Getter Methods
| Method |
Return Type |
Default |
Description
|
getTop() |
double |
1.0 |
Top margin in centimeters
|
getBottom() |
double |
1.0 |
Bottom margin in centimeters
|
getLeft() |
double |
1.0 |
Left margin in centimeters
|
getRight() |
double |
1.0 |
Right margin in centimeters
|
Serialization (toMap)
| Map Key |
Type |
Description
|
top |
double |
Top margin value
|
bottom |
double |
Bottom margin value
|
left |
double |
Left margin value
|
right |
double |
Right margin value
|
Usage Examples
// Use default margins (1.0 cm on all sides)
PageMargin defaultMargin = new PageMargin();
// Create custom margins
PageMargin customMargin = new PageMargin(2.0, 2.0, 1.5, 1.5);
// Apply to print options
PrintOptions printOptions = new PrintOptions();
printOptions.setPageMargin(customMargin);
// Create zero-margin layout
PageMargin noMargin = new PageMargin(0, 0, 0, 0);
printOptions.setPageMargin(noMargin);
// Serialize to wire format
Map<String, Object> marginMap = customMargin.toMap();
// Result: {top=2.0, bottom=2.0, left=1.5, right=1.5}
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.