Skip to content

Commit 94a85f8

Browse files
committed
Added "Default" margins and paper sizes so it's
much cleaner when working with our "baseline."
1 parent 6b9c589 commit 94a85f8

5 files changed

Lines changed: 97 additions & 53 deletions

File tree

lib/Domain/Builders/Faceted/Margins.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public enum Margins
1919
{
2020
None = 0,
2121

22-
Normal = 1,
22+
Default = 1,
2323

24-
Large = 2
24+
Normal = 2,
25+
26+
Large = 3
2527
}

lib/Domain/Dimensions/Dimension.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,8 @@ public override int GetHashCode()
192192
{
193193
return !Equals(left, right);
194194
}
195+
196+
// Implicit conversions from double and int to Dimension (defaulting to Inches)
197+
public static implicit operator Dimension(double value) => new(value, DimensionUnitType.Inches);
198+
public static implicit operator Dimension(int value) => new(value, DimensionUnitType.Inches);
195199
}

lib/Domain/Requests/Facets/PageProperties.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ namespace Gotenberg.Sharp.API.Client.Domain.Requests.Facets
2828
/// </remarks>
2929
public sealed class PageProperties : IConvertToHttpContent
3030
{
31+
public PageProperties()
32+
{
33+
var defaultPaperSize = PaperSizes.Letter.ToSelectedSize();
34+
35+
PaperWidth = defaultPaperSize.Width;
36+
PaperHeight = defaultPaperSize.Height;
37+
38+
var defaultMargins = Margins.Default.ToSelectedMargins();
39+
40+
MarginTop = defaultMargins.Top;
41+
MarginBottom = defaultMargins.Bottom;
42+
MarginLeft = defaultMargins.Left;
43+
MarginRight = defaultMargins.Right;
44+
}
45+
3146
#region Properties
3247

3348
/// <summary>
@@ -43,7 +58,7 @@ public sealed class PageProperties : IConvertToHttpContent
4358
/// The width of the paper.
4459
/// </value>
4560
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.PaperWidth)]
46-
public Dimension? PaperWidth { get; set; }
61+
public Dimension PaperWidth { get; set; }
4762

4863
/// <summary>
4964
/// Gets or sets the height of the paper.
@@ -52,7 +67,7 @@ public sealed class PageProperties : IConvertToHttpContent
5267
/// The height of the paper.
5368
/// </value>
5469
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.PaperHeight)]
55-
public Dimension? PaperHeight { get; set; }
70+
public Dimension PaperHeight { get; set; }
5671

5772
/// <summary>
5873
/// Gets or sets the margin top.
@@ -61,7 +76,7 @@ public sealed class PageProperties : IConvertToHttpContent
6176
/// The margin top.
6277
/// </value>
6378
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.MarginTop)]
64-
public Dimension? MarginTop { get; set; }
79+
public Dimension MarginTop { get; set; }
6580

6681
/// <summary>
6782
/// Gets or sets the margin bottom.
@@ -70,7 +85,7 @@ public sealed class PageProperties : IConvertToHttpContent
7085
/// The margin bottom.
7186
/// </value>
7287
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.MarginBottom)]
73-
public Dimension? MarginBottom { get; set; }
88+
public Dimension MarginBottom { get; set; }
7489

7590
/// <summary>
7691
/// Gets or sets the margin left.
@@ -79,7 +94,7 @@ public sealed class PageProperties : IConvertToHttpContent
7994
/// The margin left.
8095
/// </value>
8196
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.MarginLeft)]
82-
public Dimension? MarginLeft { get; set; }
97+
public Dimension MarginLeft { get; set; }
8398

8499
/// <summary>
85100
/// Gets or sets the margin right.
@@ -88,7 +103,7 @@ public sealed class PageProperties : IConvertToHttpContent
88103
/// The margin right.
89104
/// </value>
90105
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.MarginRight)]
91-
public Dimension? MarginRight { get; set; }
106+
public Dimension MarginRight { get; set; }
92107

93108
/// <summary>
94109
/// Gets or sets a value indicating whether this <see cref="PageProperties"/> is landscape.

lib/Extensions/DimensionHelpers.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2019-2025 Chris Mohan, Jaben Cargman
2+
// and GotenbergSharpApiClient Contributors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
using System.ComponentModel;
17+
using Gotenberg.Sharp.API.Client.Domain.Dimensions;
18+
19+
namespace Gotenberg.Sharp.API.Client.Extensions
20+
{
21+
public static class DimensionHelpers
22+
{
23+
private static readonly IEnumerable<(Margins MarginType, (Dimension Left, Dimension Right, Dimension Top, Dimension Bottom) Value)> MarginSizer =
24+
[
25+
(Margins.None, (Left: 0.0, Right: 0.0, Top: 0.0, Bottom: 0.0)),
26+
(Margins.Default, (Left: 0.39, Right: 0.39, Top: 0.39, Bottom: 0.39)),
27+
(Margins.Normal, (Left: 1.0, Right: 1.0, Top: 1.0, Bottom: 1.0)),
28+
(Margins.Large, (Left: 2.0, Right: 2.0, Top: 2.0, Bottom: 2.0))
29+
];
30+
31+
private static readonly IEnumerable<(PaperSizes Size, (Dimension Width, Dimension Height) Value)> PaperSizer =
32+
[
33+
(PaperSizes.A3, (Width: 11.7, Height: 16.5)),
34+
(PaperSizes.A4, (Width: 8.27, Height: 11.7)),
35+
(PaperSizes.A5, (Width: 5.8, Height: 8.2)),
36+
(PaperSizes.A6, (Width: 4.1, Height: 5.8)),
37+
(PaperSizes.Letter, (Width: 8.5, Height: 11.0)),
38+
(PaperSizes.Legal, (Width: 8.5, Height: 14.0)),
39+
(PaperSizes.Tabloid, (Width: 11.0, Height: 17.0))
40+
];
41+
42+
internal static (Dimension Width, Dimension Height) ToSelectedSize(this PaperSizes selectedSize)
43+
{
44+
if (!Enum.IsDefined(typeof(PaperSizes), selectedSize))
45+
throw new InvalidEnumArgumentException(
46+
nameof(selectedSize),
47+
(int)selectedSize,
48+
typeof(PaperSizes));
49+
50+
if (selectedSize == default)
51+
throw new InvalidOperationException(nameof(selectedSize));
52+
53+
return PaperSizer.First(s => s.Size == selectedSize).Value;
54+
}
55+
56+
internal static (Dimension Left, Dimension Right, Dimension Top, Dimension Bottom) ToSelectedMargins(
57+
this Margins selected)
58+
{
59+
if (!Enum.IsDefined(typeof(Margins), selected))
60+
throw new InvalidEnumArgumentException(
61+
nameof(selected),
62+
(int)selected,
63+
typeof(PaperSizes));
64+
65+
return MarginSizer.First(m => m.MarginType == selected).Value;
66+
}
67+
}
68+
}

lib/Extensions/EnumExtensions.cs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,63 +15,18 @@
1515

1616
using System.ComponentModel;
1717
using System.Reflection;
18-
using Gotenberg.Sharp.API.Client.Domain.Dimensions;
1918

2019
namespace Gotenberg.Sharp.API.Client.Extensions;
2120

2221
internal static class EnumExtensions
2322
{
24-
private static readonly IEnumerable<(Margins MarginType, (Dimension Left, Dimension Right, Dimension Top, Dimension Bottom) Value)> MarginSizer =
25-
[
26-
(Margins.None, (Left: Dimension.FromInches(0.0), Right: Dimension.FromInches(0.0), Top: Dimension.FromInches(0.0), Bottom: Dimension.FromInches(0.0))),
27-
(Margins.Normal, (Left: Dimension.FromInches(1.0), Right: Dimension.FromInches(1.0), Top: Dimension.FromInches(1.0), Bottom: Dimension.FromInches(1.0))),
28-
(Margins.Large, (Left: Dimension.FromInches(2.0), Right: Dimension.FromInches(2.0), Top: Dimension.FromInches(2.0), Bottom: Dimension.FromInches(2.0)))
29-
];
30-
31-
private static readonly IEnumerable<(PaperSizes Size, (Dimension Width, Dimension Height) Value)> PaperSizer =
32-
[
33-
(PaperSizes.A3, (Width: Dimension.FromInches(11.7), Height: Dimension.FromInches(16.5))),
34-
(PaperSizes.A4, (Width: Dimension.FromInches(8.27), Height: Dimension.FromInches(11.7))),
35-
(PaperSizes.A5, (Width: Dimension.FromInches(5.8), Height: Dimension.FromInches(8.2))),
36-
(PaperSizes.A6, (Width: Dimension.FromInches(4.1), Height: Dimension.FromInches(5.8))),
37-
(PaperSizes.Letter, (Width: Dimension.FromInches(8.5), Height: Dimension.FromInches(11.0))),
38-
(PaperSizes.Legal, (Width: Dimension.FromInches(8.5), Height: Dimension.FromInches(14.0))),
39-
(PaperSizes.Tabloid, (Width: Dimension.FromInches(11.0), Height: Dimension.FromInches(17.0)))
40-
];
41-
4223
internal static string ToFormDataValue(this PdfFormats format)
4324
{
4425
return format == default
4526
? "PDF/A-1a"
4627
: $"PDF/A-{format.ToString().Substring(1, 2)}";
4728
}
4829

49-
internal static (Dimension Width, Dimension Height) ToSelectedSize(this PaperSizes selectedSize)
50-
{
51-
if (!Enum.IsDefined(typeof(PaperSizes), selectedSize))
52-
throw new InvalidEnumArgumentException(
53-
nameof(selectedSize),
54-
(int)selectedSize,
55-
typeof(PaperSizes));
56-
57-
if (selectedSize == default)
58-
throw new InvalidOperationException(nameof(selectedSize));
59-
60-
return PaperSizer.First(s => s.Size == selectedSize).Value;
61-
}
62-
63-
internal static (Dimension Left, Dimension Right, Dimension Top, Dimension Bottom) ToSelectedMargins(
64-
this Margins selected)
65-
{
66-
if (!Enum.IsDefined(typeof(Margins), selected))
67-
throw new InvalidEnumArgumentException(
68-
nameof(selected),
69-
(int)selected,
70-
typeof(PaperSizes));
71-
72-
return MarginSizer.First(m => m.MarginType == selected).Value;
73-
}
74-
7530
public static string GetDescription(this Enum value)
7631
{
7732
FieldInfo field = value.GetType().GetField(value.ToString())!;

0 commit comments

Comments
 (0)