2424using iText . Layout . Font ;
2525using iText . Layout . Layout ;
2626using iText . Layout . Properties ;
27+ using iText . Layout . Splitting ;
2728using log4net ;
2829using NetTopologySuite . Utilities ;
30+ using static iText . Kernel . Pdf . Colorspace . PdfPattern ;
2931using Path = System . IO . Path ;
3032using Text = iText . Layout . Element . Text ;
3133
@@ -955,7 +957,6 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
955957
956958 if ( wrap || justified )
957959 {
958-
959960 bottomAux = ( float ) convertScale ( bottomOri ) ;
960961 topAux = ( float ) convertScale ( topOri ) ;
961962
@@ -969,7 +970,7 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
969970 float urx = rightAux + leftMargin ;
970971 float ury = ( float ) this . pageSize . GetTop ( ) - topAux - topMargin - bottomMargin ;
971972
972- DrawColumnText ( canvas , llx , lly , urx , ury , p , leading , runDirection , valign , alignment ) ;
973+ DrawColumnText ( canvas , llx , lly , urx , ury , p , leading , runDirection , valign , alignment , wrap ) ;
973974
974975 }
975976 else //no wrap
@@ -992,7 +993,6 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
992993 text . AddStyle ( fontStyle ) ;
993994
994995 Paragraph phrase = new Paragraph ( text ) . SetVerticalAlignment ( VerticalAlignment . TOP ) . SetFontColor ( foreColor ) ;
995-
996996 switch ( alignment )
997997 {
998998 case 1 : // Center Alignment
@@ -1006,7 +1006,7 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b
10061006 break ;
10071007 case 3 : // Justified, only one text line
10081008 cb . ShowTextAligned ( phrase , leftAux + leftMargin , this . pageSize . GetTop ( ) - bottomAux - topMargin - bottomMargin + startHeight , justifiedType ) ; //0, runDirection, arabicOptions);
1009- break ;
1009+ break ;
10101010 }
10111011 }
10121012 }
@@ -1131,19 +1131,72 @@ private VerticalAlignment GetVericalAlignment(float valign)
11311131 return null ;
11321132 }
11331133
1134- void DrawColumnText ( PdfCanvas pdfCanvas , float llx , float lly , float urx , float ury , Paragraph p , float leading , int runDirection , int valign , int alignment )
1134+ void DrawColumnText ( PdfCanvas pdfCanvas , float llx , float lly , float urx , float ury , Paragraph p , float leading , int runDirection , int valign , int alignment , Boolean wrap )
11351135 {
1136- float width = urx - llx ;
1137- float height = ury - lly ;
1138-
1139- p . SetTextAlignment ( GetTextAlignment ( alignment ) )
1140- . SetVerticalAlignment ( GetVericalAlignment ( valign ) )
1141- . SetMultipliedLeading ( MULTIPLIED_LEADING )
1142- . SetWidth ( width )
1143- . SetMaxHeight ( height ) ;
1144- //.SetMargin(10);
1145- Canvas cb = new Canvas ( pdfCanvas , new Rectangle ( llx , lly , width , height ) ) ;
1146- cb . Add ( p ) ;
1136+ if ( VerticalAlign . MIDDLE . Equals ( valign ) )
1137+ {
1138+ ury = ury + leading ;
1139+ p . SetVerticalAlignment ( VerticalAlignment . MIDDLE ) ;
1140+ }
1141+ else if ( VerticalAlign . BOTTOM . Equals ( valign ) )
1142+ {
1143+ ury = ury + leading ;
1144+ p . SetVerticalAlignment ( VerticalAlignment . BOTTOM ) ;
1145+ }
1146+ else if ( VerticalAlign . TOP . Equals ( valign ) )
1147+ {
1148+ ury = ury + leading / 2 ;
1149+ p . SetVerticalAlignment ( VerticalAlignment . TOP ) ;
1150+ }
1151+ Rectangle rect = new Rectangle ( llx , lly , urx - llx , ury - lly ) ;
1152+ p . SetTextAlignment ( GetTextAlignment ( alignment ) ) ;
1153+
1154+ if ( wrap )
1155+ {
1156+ p . SetProperty ( Property . SPLIT_CHARACTERS , new CustomSplitCharacters ( ) ) ;
1157+ Table table = new Table ( 1 ) ;
1158+ table . SetFixedPosition ( this . getPage ( ) , rect . GetX ( ) , rect . GetY ( ) , rect . GetWidth ( ) ) ;
1159+ Cell cell = new Cell ( ) ;
1160+ cell . SetWidth ( rect . GetWidth ( ) ) ;
1161+ cell . SetHeight ( rect . GetHeight ( ) ) ;
1162+ cell . SetBorder ( Border . NO_BORDER ) ;
1163+ cell . SetVerticalAlignment ( VerticalAlignment . MIDDLE ) ;
1164+ cell . Add ( p ) ;
1165+ table . AddCell ( cell ) ;
1166+ document . Add ( table ) ;
1167+ }
1168+ else
1169+ {
1170+ try
1171+ {
1172+ Canvas canvas = new Canvas ( pdfCanvas , rect ) ;
1173+ canvas . Add ( p ) ;
1174+ canvas . Close ( ) ;
1175+ }
1176+ catch ( Exception e ) { GXLogging . Error ( log , "GxDrawText failed to justify text column: " , e ) ; }
1177+ }
1178+ }
1179+
1180+ public class CustomSplitCharacters : DefaultSplitCharacters
1181+ {
1182+ public override bool IsSplitCharacter ( GlyphLine text , int glyphPos )
1183+ {
1184+ if ( ! text . Get ( glyphPos ) . HasValidUnicode ( ) )
1185+ {
1186+ return false ;
1187+ }
1188+
1189+ bool baseResult = base . IsSplitCharacter ( text , glyphPos ) ;
1190+ bool myResult = false ;
1191+ Glyph glyph = text . Get ( glyphPos ) ;
1192+
1193+ if ( glyph . GetUnicode ( ) == '_' )
1194+ {
1195+ myResult = true ;
1196+ }
1197+
1198+ return myResult || baseResult ;
1199+ }
11471200 }
11481201
11491202#pragma warning restore CS0612 // Type or member is obsolete
0 commit comments