2828import org .apache .pdfbox .pdmodel .*;
2929import org .apache .pdfbox .pdmodel .common .PDRectangle ;
3030import org .apache .pdfbox .pdmodel .font .*;
31- import org .apache .pdfbox .pdmodel .graphics .form .PDFormXObject ;
3231import org .apache .pdfbox .pdmodel .graphics .image .LosslessFactory ;
3332import org .apache .pdfbox .pdmodel .graphics .image .PDImageXObject ;
3433import org .apache .pdfbox .pdmodel .graphics .state .RenderingMode ;
@@ -61,6 +60,8 @@ public class PDFReportPDFBox extends GXReportPDFCommons{
6160 ConcurrentHashMap <String , PDImageXObject > documentImages ;
6261 public int runDirection = 0 ;
6362 private int page ;
63+ private PDPageContentStream auxContentStream ;
64+ private boolean useAuxContentStream ;
6465
6566 private final float DEFAULT_PDFBOX_LEADING = 1.2f ;
6667
@@ -228,41 +229,41 @@ private void roundRectangle(PDPageContentStream cb, float x, float y, float w, f
228229
229230 public void GxDrawRect (int left , int top , int right , int bottom , int pen , int foreRed , int foreGreen , int foreBlue , int backMode , int backRed , int backGreen , int backBlue ,
230231 int styleTop , int styleBottom , int styleRight , int styleLeft , int cornerRadioTL , int cornerRadioTR , int cornerRadioBL , int cornerRadioBR ) {
231- try (PDPageContentStream cb = getNewPDPageContentStream ()){
232-
233- float penAux = (float )convertScale (pen );
234- float rightAux = (float )convertScale (right );
235- float bottomAux = (float )convertScale (bottom );
236- float leftAux = (float )convertScale (left );
237- float topAux = (float )convertScale (top );
232+ PDPageContentStream cb = null ;
233+ try {
234+ cb = useAuxContentStream ? auxContentStream : getNewPDPageContentStream ();
235+ float penAux = (float ) convertScale (pen );
236+ float rightAux = (float ) convertScale (right );
237+ float bottomAux = (float ) convertScale (bottom );
238+ float leftAux = (float ) convertScale (left );
239+ float topAux = (float ) convertScale (top );
238240
239241 cb .saveGraphicsState ();
240242
241243 float x1 , y1 , x2 , y2 ;
242244 x1 = leftAux + leftMargin ;
243- y1 = pageSize .getUpperRightY () - bottomAux - topMargin -bottomMargin ;
245+ y1 = pageSize .getUpperRightY () - bottomAux - topMargin - bottomMargin ;
244246 x2 = rightAux + leftMargin ;
245- y2 = pageSize .getUpperRightY () - topAux - topMargin -bottomMargin ;
247+ y2 = pageSize .getUpperRightY () - topAux - topMargin - bottomMargin ;
246248
247249 cb .setLineWidth (penAux );
248250 cb .setLineCapStyle (2 );
249251
250- if (cornerRadioBL == 0 && cornerRadioBR == 0 && cornerRadioTL == 0 && cornerRadioTR == 0 && styleBottom == 0 && styleLeft == 0 && styleRight == 0 && styleTop == 0 ) {
252+ if (cornerRadioBL == 0 && cornerRadioBR == 0 && cornerRadioTL == 0 && cornerRadioTR == 0 && styleBottom == 0 && styleLeft == 0 && styleRight == 0 && styleTop == 0 ) {
251253 if (pen > 0 )
252254 cb .setStrokingColor (foreRed , foreGreen , foreBlue );
253255 else
254- cb .setStrokingColor (backRed , backGreen , backBlue );
256+ cb .setStrokingColor (backRed , backGreen , backBlue );
255257
256258 cb .addRect (x1 , y1 , x2 - x1 , y2 - y1 );
257259
258- if (backMode != 0 ) {
260+ if (backMode != 0 ) {
259261 cb .setNonStrokingColor (new Color (backRed , backGreen , backBlue ));
260262 cb .fillAndStroke ();
261263 }
262264 cb .closePath ();
263265 cb .stroke ();
264- }
265- else {
266+ } else {
266267 float w = x2 - x1 ;
267268 float h = y2 - y1 ;
268269 if (w < 0 ) {
@@ -274,18 +275,18 @@ public void GxDrawRect(int left, int top, int right, int bottom, int pen, int fo
274275 h = -h ;
275276 }
276277
277- float cRadioTL = (float )convertScale (cornerRadioTL );
278- float cRadioTR = (float )convertScale (cornerRadioTR );
279- float cRadioBL = (float )convertScale (cornerRadioBL );
280- float cRadioBR = (float )convertScale (cornerRadioBR );
278+ float cRadioTL = (float ) convertScale (cornerRadioTL );
279+ float cRadioTR = (float ) convertScale (cornerRadioTR );
280+ float cRadioBL = (float ) convertScale (cornerRadioBL );
281+ float cRadioBR = (float ) convertScale (cornerRadioBR );
281282
282- int max = (int )Math .min (w , h );
283- cRadioTL = Math .max (0 , Math .min (cRadioTL , max / 2 ));
284- cRadioTR = Math .max (0 , Math .min (cRadioTR , max / 2 ));
285- cRadioBL = Math .max (0 , Math .min (cRadioBL , max / 2 ));
286- cRadioBR = Math .max (0 , Math .min (cRadioBR , max / 2 ));
283+ int max = (int ) Math .min (w , h );
284+ cRadioTL = Math .max (0 , Math .min (cRadioTL , max / 2 ));
285+ cRadioTR = Math .max (0 , Math .min (cRadioTR , max / 2 ));
286+ cRadioBL = Math .max (0 , Math .min (cRadioBL , max / 2 ));
287+ cRadioBR = Math .max (0 , Math .min (cRadioBR , max / 2 ));
287288
288- if (backMode != 0 ) {
289+ if (backMode != 0 ) {
289290 cb .setStrokingColor (backRed , backGreen , backBlue );
290291 cb .setLineWidth (0 );
291292 roundRectangle (cb , x1 , y1 , w , h ,
@@ -308,6 +309,15 @@ public void GxDrawRect(int left, int top, int right, int bottom, int pen, int fo
308309 log .debug ("GxDrawRect -> (" + left + "," + top + ") - (" + right + "," + bottom + ") BackMode: " + backMode + " Pen:" + pen );
309310 } catch (Exception e ) {
310311 log .error ("GxDrawRect failed: " , e );
312+ } finally {
313+ try {
314+ if (cb != null && !useAuxContentStream )
315+ cb .close ();
316+ else if (useAuxContentStream )
317+ useAuxContentStream = false ;
318+ } catch (IOException ioe ) {
319+ log .error ("Failed to close content stream" , ioe );
320+ }
311321 }
312322 }
313323
@@ -574,6 +584,7 @@ public void setAsianFont(String fontName, String style) {
574584 log .error ("setAsianFont failed: " , e );
575585 }
576586 }
587+
577588 public void GxDrawText (String sTxt , int left , int top , int right , int bottom , int align , int htmlformat , int border , int valign ) {
578589 PDPageContentStream cb = null ;
579590 try {
@@ -583,6 +594,8 @@ public void GxDrawText(String sTxt, int left, int top, int right, int bottom, in
583594 printRectangle = true ;
584595
585596 if (printRectangle && (border == 1 || backFill )) {
597+ auxContentStream = cb ;
598+ useAuxContentStream = true ;
586599 GxDrawRect (left , top , right , bottom , border , foreColor .getRed (), foreColor .getGreen (), foreColor .getBlue (), backFill ? 1 : 0 , backColor .getRed (), backColor .getGreen (), backColor .getBlue (), 0 , 0 );
587600 }
588601
@@ -739,11 +752,9 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value())
739752 rectangle .setUpperRightY (this .pageSize .getUpperRightY () - topAux - topMargin -bottomMargin );
740753 break ;
741754 }
742- PDPageContentStream contentStream = getNewPDPageContentStream ();
743- contentStream .setNonStrokingColor (backColor );
744- contentStream .addRect (rectangle .getLowerLeftX (), rectangle .getLowerLeftY (),rectangle .getWidth (), rectangle .getHeight ());
745- contentStream .fill ();
746- contentStream .close ();
755+ cb .setNonStrokingColor (backColor );
756+ cb .addRect (rectangle .getLowerLeftX (), rectangle .getLowerLeftY (),rectangle .getWidth (), rectangle .getHeight ());
757+ cb .fill ();
747758 }
748759
749760 float underlineSeparation = lineHeight / 5 ;
@@ -1082,6 +1093,8 @@ private float renderHTMLContent(PDPageContentStream contentStream, String text,
10821093
10831094 private void resolveTextStyling (PDPageContentStream contentStream , String text , float x , float y , boolean isWrapped ){
10841095 try {
1096+ contentStream .setNonStrokingColor (foreColor );
1097+ contentStream .setRenderingMode (RenderingMode .FILL );
10851098 if (this .fontBold && this .fontItalic ){
10861099 contentStream .setStrokingColor (foreColor );
10871100 contentStream .setLineWidth (fontSize * 0.05f );
0 commit comments