1
1
using Synercoding . FileFormats . Pdf . Extensions ;
2
- using Synercoding . FileFormats . Pdf . LowLevel . Graphics ;
3
- using Synercoding . FileFormats . Pdf . LowLevel . Graphics . Colors ;
4
- using Synercoding . FileFormats . Pdf . LowLevel . Graphics . Colors . ColorSpaces ;
2
+ using Synercoding . FileFormats . Pdf . LowLevel . Colors ;
3
+ using Synercoding . FileFormats . Pdf . LowLevel . Colors . ColorSpaces ;
5
4
using Synercoding . FileFormats . Pdf . LowLevel . Text ;
6
5
using Synercoding . Primitives ;
7
6
using Synercoding . Primitives . Extensions ;
@@ -23,7 +22,7 @@ public static void Main(string[] args)
23
22
using ( var fs = File . OpenWrite ( fileName ) )
24
23
using ( var writer = new PdfWriter ( fs ) )
25
24
{
26
- var bleed = new Spacing ( 3 , Unit . Millimeters ) ;
25
+ var bleed = Mm ( 3 ) ;
27
26
var mediaBox = Sizes . A4 . Expand ( bleed ) . AsRectangle ( ) ;
28
27
var trimBox = mediaBox . Contract ( bleed ) ;
29
28
@@ -44,11 +43,42 @@ public static void Main(string[] args)
44
43
45
44
using ( var blurStream = File . OpenRead ( "Pexels_com/4k-wallpaper-blur-bokeh-1484253.jpg" ) )
46
45
{
47
- var addedImage = writer . AddJpgImageUnsafe ( blurStream , 7000 , 4672 ) ;
46
+ var addedImage = writer . AddJpgUnsafe ( blurStream , 7000 , 4672 , DeviceRGB . Instance ) ;
48
47
var scale = ( double ) addedImage . Width / addedImage . Height ;
49
- page . AddImage ( addedImage , new Rectangle ( 0 , 0 , scale * 303 , 303 , Unit . Millimeters ) ) ;
48
+ page . Content . AddImage ( addedImage , new Rectangle ( 0 , 0 , scale * 303 , 303 , Unit . Millimeters ) ) ;
50
49
}
51
50
} )
51
+ // Add text to page and use it as the clipping path
52
+ . AddPage ( page =>
53
+ {
54
+ page . MediaBox = mediaBox ;
55
+ page . TrimBox = trimBox ;
56
+
57
+ page . Content . WrapInState ( content =>
58
+ {
59
+ content . AddText ( textOp =>
60
+ {
61
+ textOp . SetTextRenderingMode ( TextRenderingMode . AddClippingPath )
62
+ . SetFontAndSize ( StandardFonts . Helvetica , 160 )
63
+ . SetTextLeading ( 500 )
64
+ . MoveToStartNextLine ( Mm ( 10 ) . AsRaw ( Unit . Points ) , Mm ( 200 ) . AsRaw ( Unit . Points ) )
65
+ . ShowText ( "Clipped" )
66
+ . SetFontAndSize ( StandardFonts . HelveticaBold , 650 )
67
+ . ShowTextOnNextLine ( "it!" ) ;
68
+ } ) ;
69
+
70
+ using ( var forestStream = File . OpenRead ( "Pexels_com/android-wallpaper-art-backlit-1114897.jpg" ) )
71
+ using ( var forestImage = SixLabors . ImageSharp . Image . Load ( forestStream ) )
72
+ {
73
+ var scale = ( double ) forestImage . Width / forestImage . Height ;
74
+
75
+ var matrix = Matrix . CreateScaleMatrix ( new Value ( scale * 303 , Unit . Millimeters ) . AsRaw ( Unit . Points ) , new Value ( 303 , Unit . Millimeters ) . AsRaw ( Unit . Points ) )
76
+ . Translate ( new Value ( - 100 , Unit . Millimeters ) . AsRaw ( Unit . Points ) , new Value ( 0 , Unit . Millimeters ) . AsRaw ( Unit . Points ) ) ;
77
+
78
+ page . Content . AddImage ( forestImage , matrix ) ;
79
+ }
80
+ } ) ;
81
+ } )
52
82
// Test placement using rectangle
53
83
. AddPage ( page =>
54
84
{
@@ -60,7 +90,7 @@ public static void Main(string[] args)
60
90
{
61
91
var scale = ( double ) barrenImage . Width / barrenImage . Height ;
62
92
63
- page . AddImage ( barrenImage , new Rectangle ( 0 , 0 , scale * 303 , 303 , Unit . Millimeters ) ) ;
93
+ page . Content . AddImage ( barrenImage , new Rectangle ( 0 , 0 , scale * 303 , 303 , Unit . Millimeters ) ) ;
64
94
}
65
95
66
96
using ( var eyeStream = File . OpenRead ( "Pexels_com/adult-blue-blue-eyes-865711.jpg" ) )
@@ -71,7 +101,7 @@ public static void Main(string[] args)
71
101
var height = 100 * scale ;
72
102
73
103
var offSet = 6 ;
74
- page . AddImage ( eyeStream , new Rectangle ( offSet , offSet , width + offSet , height + offSet , Unit . Millimeters ) ) ;
104
+ page . Content . AddImage ( eyeStream , new Rectangle ( offSet , offSet , width + offSet , height + offSet , Unit . Millimeters ) ) ;
75
105
}
76
106
} )
77
107
// Test shape graphics
@@ -80,40 +110,39 @@ public static void Main(string[] args)
80
110
page . MediaBox = mediaBox ;
81
111
page . TrimBox = trimBox ;
82
112
83
- page . AddShapes ( ctx =>
113
+ page . Content . AddShapes ( ctx =>
84
114
{
85
- ctx . DefaultState ( g =>
86
- {
87
- g . LineWidth = 1 ;
88
- g . Fill = null ;
89
- g . Stroke = null ;
90
- g . Dash = new Dash ( )
91
- {
92
- Array = Array . Empty < double > ( ) ,
93
- Phase = 0
94
- } ;
95
- g . MiterLimit = 10 ;
96
- g . LineCap = LineCapStyle . ButtCap ;
97
- g . LineJoin = LineJoinStyle . MiterJoin ;
98
- } ) ;
115
+ ctx . SetMiterLimit ( 10 )
116
+ . SetLineCap ( LowLevel . Graphics . LineCapStyle . ButtCap )
117
+ . SetLineJoin ( LowLevel . Graphics . LineJoinStyle . MiterJoin ) ;
99
118
100
- ctx . NewPath ( g => { g . Fill = PredefinedColors . Red ; g . Stroke = PredefinedColors . Black ; g . LineWidth = 5 ; } )
101
- . Move ( 100 , 100 )
119
+ ctx . Move ( 100 , 100 )
102
120
. LineTo ( 200 , 100 )
103
121
. LineTo ( 200 , 200 )
104
- . LineTo ( 100 , 200 ) ;
105
- ctx . NewPath ( g => { g . Fill = PredefinedColors . Blue ; g . Stroke = null ; } )
106
- . Move ( 50 , 50 )
122
+ . LineTo ( 100 , 200 )
123
+ . SetLineWidth ( 5 )
124
+ . SetStroke ( PredefinedColors . Black )
125
+ . SetFill ( PredefinedColors . Red )
126
+ . FillThenStroke ( LowLevel . FillRule . NonZeroWindingNumber ) ;
127
+
128
+ ctx . Move ( 50 , 50 )
107
129
. LineTo ( 150 , 50 )
108
130
. LineTo ( 150 , 150 )
109
131
. LineTo ( 50 , 150 )
110
- . Close ( ) ;
111
- ctx . NewPath ( g => { g . Fill = null ; g . Stroke = PredefinedColors . Yellow ; g . LineWidth = 3 ; g . Dash = new Dash ( ) { Array = new [ ] { 5d } } ; } )
112
- . Move ( 150 , 150 )
132
+ . SetLineWidth ( 1 )
133
+ . SetFill ( PredefinedColors . Blue )
134
+ . CloseSubPath ( )
135
+ . Fill ( LowLevel . FillRule . NonZeroWindingNumber ) ;
136
+
137
+ ctx . Move ( 150 , 150 )
113
138
. LineTo ( 250 , 150 )
114
139
. LineTo ( 250 , 250 )
115
140
. LineTo ( 150 , 250 )
116
- . Close ( ) ;
141
+ . SetLineWidth ( 3 )
142
+ . SetStroke ( PredefinedColors . Yellow )
143
+ . SetDashPattern ( new LowLevel . Graphics . Dash ( ) { Array = new [ ] { 5d } } )
144
+ . CloseSubPath ( )
145
+ . Stroke ( ) ;
117
146
} ) ;
118
147
} )
119
148
// Test pages with text
@@ -122,23 +151,26 @@ public static void Main(string[] args)
122
151
page . MediaBox = mediaBox ;
123
152
page . TrimBox = trimBox ;
124
153
125
- page . AddText ( "The quick brown fox jumps over the lazy dog." , new Point ( Mm ( 10 ) , Mm ( 10 ) ) , new TextState ( StandardFonts . Helvetica , 12 )
154
+ page . Content . AddText ( ops =>
126
155
{
127
- Fill = PredefinedColors . Blue
156
+ ops . MoveToStartNextLine ( Mm ( 10 ) . AsRaw ( Unit . Points ) , Mm ( 10 ) . AsRaw ( Unit . Points ) )
157
+ . SetFontAndSize ( StandardFonts . Helvetica , 12 )
158
+ . SetFill ( PredefinedColors . Blue )
159
+ . ShowText ( "The quick brown fox jumps over the lazy dog." ) ;
128
160
} ) ;
129
- page . AddText ( "Text with a newline" + Environment . NewLine + "in it." , new Point ( Mm ( 10 ) , Mm ( 20 ) ) ) ;
161
+
162
+ page . Content . AddText ( "Text with a newline" + Environment . NewLine + "in it." , StandardFonts . Helvetica , 12 , new Point ( Mm ( 10 ) , Mm ( 20 ) ) ) ;
130
163
} )
131
164
. AddPage ( page =>
132
165
{
133
166
page . MediaBox = mediaBox ;
134
167
page . TrimBox = trimBox ;
135
168
136
- page . AddText ( "This page also used Helvetica" , new Point ( Mm ( 10 ) , Mm ( 10 ) ) , state =>
169
+ page . Content . AddText ( "This page also used Helvetica" , StandardFonts . Helvetica , 32 , textContext =>
137
170
{
138
- state . FontSize = 32 ;
139
- state . Font = StandardFonts . Helvetica ;
140
- state . RenderingMode = TextRenderingMode . Stroke ;
141
- state . Stroke = PredefinedColors . Red ;
171
+ textContext . MoveToStartNextLine ( Mm ( 10 ) . AsRaw ( Unit . Points ) , Mm ( 10 ) . AsRaw ( Unit . Points ) )
172
+ . SetTextRenderingMode ( TextRenderingMode . Stroke )
173
+ . SetStroke ( PredefinedColors . Red ) ;
142
174
} ) ;
143
175
} )
144
176
// Test placement using matrix
@@ -155,7 +187,7 @@ public static void Main(string[] args)
155
187
var matrix = Matrix . CreateScaleMatrix ( new Value ( scale * 303 , Unit . Millimeters ) . AsRaw ( Unit . Points ) , new Value ( 303 , Unit . Millimeters ) . AsRaw ( Unit . Points ) )
156
188
. Translate ( new Value ( - 100 , Unit . Millimeters ) . AsRaw ( Unit . Points ) , new Value ( 0 , Unit . Millimeters ) . AsRaw ( Unit . Points ) ) ;
157
189
158
- page . AddImage ( forestImage , matrix ) ;
190
+ page . Content . AddImage ( forestImage , matrix ) ;
159
191
}
160
192
} ) ;
161
193
@@ -173,7 +205,7 @@ public static void Main(string[] args)
173
205
174
206
var scale = ( double ) blurImage . Width / blurImage . Height ;
175
207
176
- page . AddImage ( reusedImage , new Rectangle ( 0 , 0 , scale * 303 , 303 , Unit . Millimeters ) ) ;
208
+ page . Content . AddImage ( reusedImage , new Rectangle ( 0 , 0 , scale * 303 , 303 , Unit . Millimeters ) ) ;
177
209
} ) ;
178
210
}
179
211
@@ -185,15 +217,13 @@ public static void Main(string[] args)
185
217
186
218
var scale = ( double ) blurImage . Width / blurImage . Height ;
187
219
188
- page . AddImage ( reusedImage , new Rectangle ( 0 , 0 , scale * 303 , 303 , Unit . Millimeters ) ) ;
220
+ page . Content . AddImage ( reusedImage , new Rectangle ( 0 , 0 , scale * 303 , 303 , Unit . Millimeters ) ) ;
189
221
190
- page . AddShapes ( trimBox , static ( trim , context ) =>
222
+ page . Content . AddShapes ( trimBox , static ( trim , context ) =>
191
223
{
192
- context . DefaultState ( state =>
193
- {
194
- state . Stroke = new SpotColor ( new Separation ( LowLevel . PdfName . Get ( "CutContour" ) , PredefinedColors . Magenta ) , 1 ) ;
195
- } ) ;
196
- context . NewPath ( ) . Rectangle ( trim ) ;
224
+ context . SetStroke ( new SpotColor ( new Separation ( LowLevel . PdfName . Get ( "CutContour" ) , PredefinedColors . Magenta ) , 1 ) ) ;
225
+ context . Rectangle ( trim ) ;
226
+ context . Stroke ( ) ;
197
227
} ) ;
198
228
} ) ;
199
229
}
0 commit comments