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+ }
0 commit comments