28
28
import org .apache .pdfbox .pdmodel .*;
29
29
import org .apache .pdfbox .pdmodel .common .PDRectangle ;
30
30
import org .apache .pdfbox .pdmodel .font .*;
31
+ import org .apache .pdfbox .pdmodel .graphics .form .PDFormXObject ;
31
32
import org .apache .pdfbox .pdmodel .graphics .image .LosslessFactory ;
32
33
import org .apache .pdfbox .pdmodel .graphics .image .PDImageXObject ;
33
34
import org .apache .pdfbox .pdmodel .graphics .state .RenderingMode ;
@@ -60,8 +61,6 @@ public class PDFReportPDFBox extends GXReportPDFCommons{
60
61
ConcurrentHashMap <String , PDImageXObject > documentImages ;
61
62
public int runDirection = 0 ;
62
63
private int page ;
63
- private PDPageContentStream auxContentStream ;
64
- private boolean useAuxContentStream ;
65
64
66
65
private final float DEFAULT_PDFBOX_LEADING = 1.2f ;
67
66
@@ -229,41 +228,41 @@ private void roundRectangle(PDPageContentStream cb, float x, float y, float w, f
229
228
230
229
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 ,
231
230
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 );
240
238
241
239
cb .saveGraphicsState ();
242
240
243
241
float x1 , y1 , x2 , y2 ;
244
242
x1 = leftAux + leftMargin ;
245
- y1 = pageSize .getUpperRightY () - bottomAux - topMargin - bottomMargin ;
243
+ y1 = pageSize .getUpperRightY () - bottomAux - topMargin -bottomMargin ;
246
244
x2 = rightAux + leftMargin ;
247
- y2 = pageSize .getUpperRightY () - topAux - topMargin - bottomMargin ;
245
+ y2 = pageSize .getUpperRightY () - topAux - topMargin -bottomMargin ;
248
246
249
247
cb .setLineWidth (penAux );
250
248
cb .setLineCapStyle (2 );
251
249
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 ) {
253
251
if (pen > 0 )
254
252
cb .setStrokingColor (foreRed , foreGreen , foreBlue );
255
253
else
256
- cb .setStrokingColor (backRed , backGreen , backBlue );
254
+ cb .setStrokingColor (backRed , backGreen , backBlue );
257
255
258
256
cb .addRect (x1 , y1 , x2 - x1 , y2 - y1 );
259
257
260
- if (backMode != 0 ) {
258
+ if (backMode != 0 ) {
261
259
cb .setNonStrokingColor (new Color (backRed , backGreen , backBlue ));
262
260
cb .fillAndStroke ();
263
261
}
264
262
cb .closePath ();
265
263
cb .stroke ();
266
- } else {
264
+ }
265
+ else {
267
266
float w = x2 - x1 ;
268
267
float h = y2 - y1 ;
269
268
if (w < 0 ) {
@@ -275,18 +274,18 @@ public void GxDrawRect(int left, int top, int right, int bottom, int pen, int fo
275
274
h = -h ;
276
275
}
277
276
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 );
282
281
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 ));
288
287
289
- if (backMode != 0 ) {
288
+ if (backMode != 0 ) {
290
289
cb .setStrokingColor (backRed , backGreen , backBlue );
291
290
cb .setLineWidth (0 );
292
291
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
309
308
log .debug ("GxDrawRect -> (" + left + "," + top + ") - (" + right + "," + bottom + ") BackMode: " + backMode + " Pen:" + pen );
310
309
} catch (Exception e ) {
311
310
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
- }
321
311
}
322
312
}
323
313
@@ -584,7 +574,6 @@ public void setAsianFont(String fontName, String style) {
584
574
log .error ("setAsianFont failed: " , e );
585
575
}
586
576
}
587
-
588
577
public void GxDrawText (String sTxt , int left , int top , int right , int bottom , int align , int htmlformat , int border , int valign ) {
589
578
PDPageContentStream cb = null ;
590
579
try {
@@ -594,8 +583,6 @@ public void GxDrawText(String sTxt, int left, int top, int right, int bottom, in
594
583
printRectangle = true ;
595
584
596
585
if (printRectangle && (border == 1 || backFill )) {
597
- auxContentStream = cb ;
598
- useAuxContentStream = true ;
599
586
GxDrawRect (left , top , right , bottom , border , foreColor .getRed (), foreColor .getGreen (), foreColor .getBlue (), backFill ? 1 : 0 , backColor .getRed (), backColor .getGreen (), backColor .getBlue (), 0 , 0 );
600
587
}
601
588
@@ -752,9 +739,11 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value())
752
739
rectangle .setUpperRightY (this .pageSize .getUpperRightY () - topAux - topMargin -bottomMargin );
753
740
break ;
754
741
}
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 ();
758
747
}
759
748
760
749
float underlineSeparation = lineHeight / 5 ;
@@ -1093,8 +1082,6 @@ private float renderHTMLContent(PDPageContentStream contentStream, String text,
1093
1082
1094
1083
private void resolveTextStyling (PDPageContentStream contentStream , String text , float x , float y , boolean isWrapped ){
1095
1084
try {
1096
- contentStream .setNonStrokingColor (foreColor );
1097
- contentStream .setRenderingMode (RenderingMode .FILL );
1098
1085
if (this .fontBold && this .fontItalic ){
1099
1086
contentStream .setStrokingColor (foreColor );
1100
1087
contentStream .setLineWidth (fontSize * 0.05f );
0 commit comments