Skip to content

Commit 28706d7

Browse files
committed
[dotnet] Enhance PrintOptions class to support for predefined and custom Paper Sizes
1 parent 928833e commit 28706d7

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

dotnet/src/webdriver/PrintOptions.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,24 @@ public class PrintOptions
5050
private const double DefaultPageHeight = 21.59;
5151
private const double DefaultPageWidth = 27.94;
5252
private const double CentimetersPerInch = 2.54;
53-
53+
public static PageSize A4 { get; } = new PageSize { Width = 21.0, Height = 29.7 }; // cm
54+
public static PageSize Legal { get; } = new PageSize { Width = 21.59, Height = 35.56 }; // cm
55+
public static PageSize Letter { get; } = new PageSize { Width = 21.59, Height = 27.94 }; // cm
56+
public static PageSize Tabloid { get; } = new PageSize { Width = 27.94, Height = 43.18 }; // cm
5457
private double scale = 1.0;
5558
private PageSize pageSize = new PageSize();
5659
private Margins margins = new Margins();
5760
private readonly HashSet<object> pageRanges = new HashSet<object>();
5861

62+
/// <summary>
63+
/// Initializes a new instance of the <see cref="PrintOptions"/> class with default values.
64+
/// Default page size is set to A4.
65+
/// </summary>
66+
public PrintOptions()
67+
{
68+
this.PageDimensions = A4; // Default to A4 page size
69+
}
70+
5971
/// <summary>
6072
/// Gets or sets the orientation of the pages in the printed document.
6173
/// </summary>
@@ -99,6 +111,21 @@ public PageSize PageDimensions
99111
set => this.pageSize = value ?? throw new ArgumentNullException(nameof(value));
100112
}
101113

114+
115+
/// <summary>
116+
/// Sets the page size to a predefined or custom size.
117+
/// </summary>
118+
/// <param name="pageSize">The page size to set.</param>
119+
/// <exception cref="ArgumentNullException">Thrown if pageSize is null.</exception>
120+
public void SetPageSize(PageSize pageSize)
121+
{
122+
if (pageSize == null)
123+
{
124+
throw new ArgumentNullException(nameof(pageSize), "Page size cannot be null.");
125+
}
126+
this.PageDimensions = pageSize;
127+
}
128+
102129
/// <summary>
103130
/// Gets or sets the margins for each page in the doucment.
104131
/// </summary>

dotnet/test/common/PrintTest.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,56 @@ public void MarginsCannotHaveNegativeValues()
122122
Assert.That(() => new PrintOptions.Margins { Left = -1 }, Throws.TypeOf<ArgumentOutOfRangeException>());
123123
Assert.That(() => new PrintOptions.Margins { Right = -1 }, Throws.TypeOf<ArgumentOutOfRangeException>());
124124
}
125+
126+
[Test]
127+
public void DefaultPageSizeIsA4()
128+
{
129+
var options = new PrintOptions();
130+
131+
Assert.That(options.PageDimensions.Width, Is.EqualTo(PrintOptions.A4.Width));
132+
Assert.That(options.PageDimensions.Height, Is.EqualTo(PrintOptions.A4.Height));
133+
}
134+
135+
[Test]
136+
public void CanSetPredefinedPageSizes()
137+
{
138+
var options = new PrintOptions();
139+
140+
options.SetPageSize(PrintOptions.A4);
141+
Assert.That(options.PageDimensions.Width, Is.EqualTo(PrintOptions.A4.Width));
142+
Assert.That(options.PageDimensions.Height, Is.EqualTo(PrintOptions.A4.Height));
143+
144+
options.SetPageSize(PrintOptions.Legal);
145+
Assert.That(options.PageDimensions.Width, Is.EqualTo(PrintOptions.Legal.Width));
146+
Assert.That(options.PageDimensions.Height, Is.EqualTo(PrintOptions.Legal.Height));
147+
148+
options.SetPageSize(PrintOptions.Letter);
149+
Assert.That(options.PageDimensions.Width, Is.EqualTo(PrintOptions.Letter.Width));
150+
Assert.That(options.PageDimensions.Height, Is.EqualTo(PrintOptions.Letter.Height));
151+
152+
options.SetPageSize(PrintOptions.Tabloid);
153+
Assert.That(options.PageDimensions.Width, Is.EqualTo(PrintOptions.Tabloid.Width));
154+
Assert.That(options.PageDimensions.Height, Is.EqualTo(PrintOptions.Tabloid.Height));
155+
}
156+
157+
[Test]
158+
public void CanSetCustomPageSize()
159+
{
160+
var options = new PrintOptions();
161+
var customPageSize = new PrintOptions.PageSize { Width = 25.0, Height = 30.0 };
162+
163+
options.SetPageSize(customPageSize);
164+
165+
Assert.That(options.PageDimensions.Width, Is.EqualTo(25.0));
166+
Assert.That(options.PageDimensions.Height, Is.EqualTo(30.0));
167+
}
168+
169+
[Test]
170+
public void SettingPageSizeToNullThrowsException()
171+
{
172+
var options = new PrintOptions();
173+
Assert.That(() => options.SetPageSize(null), Throws.InstanceOf<ArgumentNullException>());
174+
}
175+
125176
}
126177
}

0 commit comments

Comments
 (0)