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 ;
3132import org .apache .pdfbox .pdmodel .graphics .image .LosslessFactory ;
3233import org .apache .pdfbox .pdmodel .graphics .image .PDImageXObject ;
3334import org .apache .pdfbox .pdmodel .graphics .state .RenderingMode ;
@@ -60,8 +61,6 @@ public class PDFReportPDFBox extends GXReportPDFCommons{
6061 ConcurrentHashMap <String , PDImageXObject > documentImages ;
6162 public int runDirection = 0 ;
6263 private int page ;
63- private PDPageContentStream auxContentStream ;
64- private boolean useAuxContentStream ;
6564
6665 private final float DEFAULT_PDFBOX_LEADING = 1.2f ;
6766
@@ -229,41 +228,41 @@ private void roundRectangle(PDPageContentStream cb, float x, float y, float w, f
229228
230229 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 ,
231230 int styleTop , int styleBottom , int styleRight , int styleLeft , int cornerRadioTL , int cornerRadioTR , int cornerRadioBL , int cornerRadioBR ) {
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 );
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 );
240238
241239 cb .saveGraphicsState ();
242240
243241 float x1 , y1 , x2 , y2 ;
244242 x1 = leftAux + leftMargin ;
245- y1 = pageSize .getUpperRightY () - bottomAux - topMargin - bottomMargin ;
243+ y1 = pageSize .getUpperRightY () - bottomAux - topMargin -bottomMargin ;
246244 x2 = rightAux + leftMargin ;
247- y2 = pageSize .getUpperRightY () - topAux - topMargin - bottomMargin ;
245+ y2 = pageSize .getUpperRightY () - topAux - topMargin -bottomMargin ;
248246
249247 cb .setLineWidth (penAux );
250248 cb .setLineCapStyle (2 );
251249
252- if (cornerRadioBL == 0 && cornerRadioBR == 0 && cornerRadioTL == 0 && cornerRadioTR == 0 && styleBottom == 0 && styleLeft == 0 && styleRight == 0 && styleTop == 0 ) {
250+ if (cornerRadioBL == 0 && cornerRadioBR == 0 && cornerRadioTL == 0 && cornerRadioTR == 0 && styleBottom == 0 && styleLeft == 0 && styleRight == 0 && styleTop == 0 ) {
253251 if (pen > 0 )
254252 cb .setStrokingColor (foreRed , foreGreen , foreBlue );
255253 else
256- cb .setStrokingColor (backRed , backGreen , backBlue );
254+ cb .setStrokingColor (backRed , backGreen , backBlue );
257255
258256 cb .addRect (x1 , y1 , x2 - x1 , y2 - y1 );
259257
260- if (backMode != 0 ) {
258+ if (backMode != 0 ) {
261259 cb .setNonStrokingColor (new Color (backRed , backGreen , backBlue ));
262260 cb .fillAndStroke ();
263261 }
264262 cb .closePath ();
265263 cb .stroke ();
266- } else {
264+ }
265+ else {
267266 float w = x2 - x1 ;
268267 float h = y2 - y1 ;
269268 if (w < 0 ) {
@@ -275,18 +274,18 @@ public void GxDrawRect(int left, int top, int right, int bottom, int pen, int fo
275274 h = -h ;
276275 }
277276
278- float cRadioTL = (float ) convertScale (cornerRadioTL );
279- float cRadioTR = (float ) convertScale (cornerRadioTR );
280- float cRadioBL = (float ) convertScale (cornerRadioBL );
281- float cRadioBR = (float ) convertScale (cornerRadioBR );
277+ float cRadioTL = (float )convertScale (cornerRadioTL );
278+ float cRadioTR = (float )convertScale (cornerRadioTR );
279+ float cRadioBL = (float )convertScale (cornerRadioBL );
280+ float cRadioBR = (float )convertScale (cornerRadioBR );
282281
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 ));
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 ));
288287
289- if (backMode != 0 ) {
288+ if (backMode != 0 ) {
290289 cb .setStrokingColor (backRed , backGreen , backBlue );
291290 cb .setLineWidth (0 );
292291 roundRectangle (cb , x1 , y1 , w , h ,
@@ -309,15 +308,6 @@ public void GxDrawRect(int left, int top, int right, int bottom, int pen, int fo
309308 log .debug ("GxDrawRect -> (" + left + "," + top + ") - (" + right + "," + bottom + ") BackMode: " + backMode + " Pen:" + pen );
310309 } catch (Exception e ) {
311310 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- }
321311 }
322312 }
323313
@@ -584,7 +574,6 @@ public void setAsianFont(String fontName, String style) {
584574 log .error ("setAsianFont failed: " , e );
585575 }
586576 }
587-
588577 public void GxDrawText (String sTxt , int left , int top , int right , int bottom , int align , int htmlformat , int border , int valign ) {
589578 PDPageContentStream cb = null ;
590579 try {
@@ -594,8 +583,6 @@ public void GxDrawText(String sTxt, int left, int top, int right, int bottom, in
594583 printRectangle = true ;
595584
596585 if (printRectangle && (border == 1 || backFill )) {
597- auxContentStream = cb ;
598- useAuxContentStream = true ;
599586 GxDrawRect (left , top , right , bottom , border , foreColor .getRed (), foreColor .getGreen (), foreColor .getBlue (), backFill ? 1 : 0 , backColor .getRed (), backColor .getGreen (), backColor .getBlue (), 0 , 0 );
600587 }
601588
@@ -752,9 +739,11 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value())
752739 rectangle .setUpperRightY (this .pageSize .getUpperRightY () - topAux - topMargin -bottomMargin );
753740 break ;
754741 }
755- cb .setNonStrokingColor (backColor );
756- cb .addRect (rectangle .getLowerLeftX (), rectangle .getLowerLeftY (),rectangle .getWidth (), rectangle .getHeight ());
757- cb .fill ();
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 ();
758747 }
759748
760749 float underlineSeparation = lineHeight / 5 ;
@@ -1093,8 +1082,6 @@ private float renderHTMLContent(PDPageContentStream contentStream, String text,
10931082
10941083 private void resolveTextStyling (PDPageContentStream contentStream , String text , float x , float y , boolean isWrapped ){
10951084 try {
1096- contentStream .setNonStrokingColor (foreColor );
1097- contentStream .setRenderingMode (RenderingMode .FILL );
10981085 if (this .fontBold && this .fontItalic ){
10991086 contentStream .setStrokingColor (foreColor );
11001087 contentStream .setLineWidth (fontSize * 0.05f );
0 commit comments