11using 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 ;
54using Synercoding . FileFormats . Pdf . LowLevel . Text ;
65using Synercoding . Primitives ;
76using Synercoding . Primitives . Extensions ;
@@ -23,7 +22,7 @@ public static void Main(string[] args)
2322 using ( var fs = File . OpenWrite ( fileName ) )
2423 using ( var writer = new PdfWriter ( fs ) )
2524 {
26- var bleed = new Spacing ( 3 , Unit . Millimeters ) ;
25+ var bleed = Mm ( 3 ) ;
2726 var mediaBox = Sizes . A4 . Expand ( bleed ) . AsRectangle ( ) ;
2827 var trimBox = mediaBox . Contract ( bleed ) ;
2928
@@ -44,11 +43,42 @@ public static void Main(string[] args)
4443
4544 using ( var blurStream = File . OpenRead ( "Pexels_com/4k-wallpaper-blur-bokeh-1484253.jpg" ) )
4645 {
47- var addedImage = writer . AddJpgImageUnsafe ( blurStream , 7000 , 4672 ) ;
46+ var addedImage = writer . AddJpgUnsafe ( blurStream , 7000 , 4672 , DeviceRGB . Instance ) ;
4847 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 ) ) ;
5049 }
5150 } )
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+ } )
5282 // Test placement using rectangle
5383 . AddPage ( page =>
5484 {
@@ -60,7 +90,7 @@ public static void Main(string[] args)
6090 {
6191 var scale = ( double ) barrenImage . Width / barrenImage . Height ;
6292
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 ) ) ;
6494 }
6595
6696 using ( var eyeStream = File . OpenRead ( "Pexels_com/adult-blue-blue-eyes-865711.jpg" ) )
@@ -71,7 +101,7 @@ public static void Main(string[] args)
71101 var height = 100 * scale ;
72102
73103 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 ) ) ;
75105 }
76106 } )
77107 // Test shape graphics
@@ -80,40 +110,39 @@ public static void Main(string[] args)
80110 page . MediaBox = mediaBox ;
81111 page . TrimBox = trimBox ;
82112
83- page . AddShapes ( ctx =>
113+ page . Content . AddShapes ( ctx =>
84114 {
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 ) ;
99118
100- ctx . NewPath ( g => { g . Fill = PredefinedColors . Red ; g . Stroke = PredefinedColors . Black ; g . LineWidth = 5 ; } )
101- . Move ( 100 , 100 )
119+ ctx . Move ( 100 , 100 )
102120 . LineTo ( 200 , 100 )
103121 . 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 )
107129 . LineTo ( 150 , 50 )
108130 . LineTo ( 150 , 150 )
109131 . 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 )
113138 . LineTo ( 250 , 150 )
114139 . LineTo ( 250 , 250 )
115140 . 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 ( ) ;
117146 } ) ;
118147 } )
119148 // Test pages with text
@@ -122,23 +151,26 @@ public static void Main(string[] args)
122151 page . MediaBox = mediaBox ;
123152 page . TrimBox = trimBox ;
124153
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 =>
126155 {
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." ) ;
128160 } ) ;
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 ) ) ) ;
130163 } )
131164 . AddPage ( page =>
132165 {
133166 page . MediaBox = mediaBox ;
134167 page . TrimBox = trimBox ;
135168
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 =>
137170 {
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 ) ;
142174 } ) ;
143175 } )
144176 // Test placement using matrix
@@ -155,7 +187,7 @@ public static void Main(string[] args)
155187 var matrix = Matrix . CreateScaleMatrix ( new Value ( scale * 303 , Unit . Millimeters ) . AsRaw ( Unit . Points ) , new Value ( 303 , Unit . Millimeters ) . AsRaw ( Unit . Points ) )
156188 . Translate ( new Value ( - 100 , Unit . Millimeters ) . AsRaw ( Unit . Points ) , new Value ( 0 , Unit . Millimeters ) . AsRaw ( Unit . Points ) ) ;
157189
158- page . AddImage ( forestImage , matrix ) ;
190+ page . Content . AddImage ( forestImage , matrix ) ;
159191 }
160192 } ) ;
161193
@@ -173,7 +205,7 @@ public static void Main(string[] args)
173205
174206 var scale = ( double ) blurImage . Width / blurImage . Height ;
175207
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 ) ) ;
177209 } ) ;
178210 }
179211
@@ -185,15 +217,13 @@ public static void Main(string[] args)
185217
186218 var scale = ( double ) blurImage . Width / blurImage . Height ;
187219
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 ) ) ;
189221
190- page . AddShapes ( trimBox , static ( trim , context ) =>
222+ page . Content . AddShapes ( trimBox , static ( trim , context ) =>
191223 {
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 ( ) ;
197227 } ) ;
198228 } ) ;
199229 }
0 commit comments