11using Synercoding . FileFormats . Pdf . Internals ;
2+ using Synercoding . FileFormats . Pdf . LowLevel . Graphics ;
3+ using Synercoding . FileFormats . Pdf . LowLevel . Graphics . Colors ;
4+ using Synercoding . FileFormats . Pdf . LowLevel . Operators . Color ;
5+ using Synercoding . FileFormats . Pdf . LowLevel . Operators . State ;
6+ using Synercoding . FileFormats . Pdf . LowLevel . Text ;
27using Synercoding . Primitives ;
38using System ;
49using System . IO ;
@@ -10,6 +15,84 @@ namespace Synercoding.FileFormats.Pdf.Extensions
1015 /// </summary>
1116 public static class PdfPageExtensions
1217 {
18+ /// <summary>
19+ /// Add text to the page.
20+ /// </summary>
21+ /// <param name="page">The page to add the text to.</param>
22+ /// <param name="text">The text to add.</param>
23+ /// <param name="point">The location of the text.</param>
24+ /// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
25+ public static PdfPage AddText ( this PdfPage page , string text , Point point )
26+ => page . AddText ( text , point , new TextState ( ) ) ;
27+
28+ /// <summary>
29+ /// Add text to the page.
30+ /// </summary>
31+ /// <param name="page">The page to add the text to.</param>
32+ /// <param name="text">The text to add.</param>
33+ /// <param name="point">The location of the text.</param>
34+ /// <param name="configureState">Configure the state of the text to place.</param>
35+ /// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
36+ public static PdfPage AddText ( this PdfPage page , string text , Point point , Action < TextState > configureState )
37+ {
38+ var state = new TextState ( ) ;
39+ configureState ( state ) ;
40+
41+ return page . AddText ( text , point , state ) ;
42+ }
43+
44+ /// <summary>
45+ /// Add text to the page.
46+ /// </summary>
47+ /// <param name="page">The page to add the text to.</param>
48+ /// <param name="text">The text to add.</param>
49+ /// <param name="point">The location of the text.</param>
50+ /// <param name="state">The state of the text to place.</param>
51+ /// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
52+ public static PdfPage AddText ( this PdfPage page , string text , Point point , TextState state )
53+ {
54+ page . MarkStdFontAsUsed ( state . Font ) ;
55+
56+ page . ContentStream
57+ . SaveState ( )
58+ . BeginText ( )
59+ . SetTextPosition ( point )
60+ . SetFontAndSize ( state . Font . LookupName , state . FontSize )
61+ . SetTextLeading ( state . Leading ?? state . FontSize ) ;
62+
63+ if ( state . Fill is Color fill )
64+ page . ContentStream . SetColorFill ( fill ) ;
65+ if ( state . Stroke is Color stroke )
66+ page . ContentStream . SetColorStroke ( stroke ) ;
67+ if ( state . LineWidth is double lineWidth )
68+ page . ContentStream . Write ( new LineWidthOperator ( lineWidth ) ) ;
69+ if ( state . LineCap is LineCapStyle lineCapStyle )
70+ page . ContentStream . Write ( new LineCapOperator ( lineCapStyle ) ) ;
71+ if ( state . LineJoin is LineJoinStyle lineJoinStyle )
72+ page . ContentStream . Write ( new LineJoinOperator ( lineJoinStyle ) ) ;
73+ if ( state . MiterLimit is double miterLimit )
74+ page . ContentStream . Write ( new MiterLimitOperator ( miterLimit ) ) ;
75+ if ( state . Dash is Dash dash )
76+ page . ContentStream . Write ( new DashOperator ( dash . Array , dash . Phase ) ) ;
77+ if ( state . CharacterSpacing is float charSpace )
78+ page . ContentStream . SetCharacterSpacing ( charSpace ) ;
79+ if ( state . WordSpacing is float wordSpace )
80+ page . ContentStream . SetWordSpacing ( wordSpace ) ;
81+ if ( state . HorizontalScaling is float horizontalScaling )
82+ page . ContentStream . SetHorizontalScaling ( horizontalScaling ) ;
83+ if ( state . TextRise is float textRise )
84+ page . ContentStream . SetTextRise ( textRise ) ;
85+ if ( state . RenderingMode is TextRenderingMode textRenderingMode )
86+ page . ContentStream . SetTextRenderMode ( textRenderingMode ) ;
87+
88+ page . ContentStream
89+ . ShowText ( text )
90+ . EndText ( )
91+ . RestoreState ( ) ;
92+
93+ return page ;
94+ }
95+
1396 /// <summary>
1497 /// Add an image to the pdf page
1598 /// </summary>
0 commit comments