4444public class PDFReportPDFBox extends GXReportPDFCommons {
4545 private PDRectangle pageSize ;
4646 private PDFont baseFont ;
47+ private String baseFontName ;
4748 private BitMatrix barcode = null ;
4849 private String barcodeType = null ;
4950 private PDDocument document ;
@@ -56,12 +57,9 @@ public class PDFReportPDFBox extends GXReportPDFCommons{
5657 ConcurrentHashMap <String , PDImageXObject > documentImages ;
5758 public int runDirection = 0 ;
5859 private int page ;
59- private PDPageContentStream auxContentStream ;
60- private boolean useAuxContentStream ;
61-
6260 private final float DEFAULT_PDFBOX_LEADING = 1.2f ;
63-
6461 private Set <String > supportedHTMLTags = new HashSet <>();
62+ private PDPageContentStream currentPageContentStream ;
6563
6664 static {
6765 log = org .apache .logging .log4j .LogManager .getLogger (PDFReportPDFBox .class );
@@ -221,9 +219,8 @@ private void roundRectangle(PDPageContentStream cb, float x, float y, float w, f
221219
222220 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 ,
223221 int styleTop , int styleBottom , int styleRight , int styleLeft , int cornerRadioTL , int cornerRadioTR , int cornerRadioBL , int cornerRadioBR ) {
224- PDPageContentStream cb = null ;
225- try {
226- cb = useAuxContentStream ? auxContentStream : currentPageContentStream ;
222+ try {
223+ PDPageContentStream cb = currentPageContentStream ;
227224 float penAux = (float ) convertScale (pen );
228225 float rightAux = (float ) convertScale (right );
229226 float bottomAux = (float ) convertScale (bottom );
@@ -301,15 +298,6 @@ public void GxDrawRect(int left, int top, int right, int bottom, int pen, int fo
301298 log .debug ("GxDrawRect -> (" + left + "," + top + ") - (" + right + "," + bottom + ") BackMode: " + backMode + " Pen:" + pen );
302299 } catch (Exception e ) {
303300 log .error ("GxDrawRect failed: " , e );
304- } finally {
305- try {
306- if (cb != null && !useAuxContentStream )
307- cb .close ();
308- else if (useAuxContentStream )
309- useAuxContentStream = false ;
310- } catch (IOException ioe ) {
311- log .error ("Failed to close content stream" , ioe );
312- }
313301 }
314302 }
315303
@@ -427,23 +415,6 @@ protected boolean removeEldestEntry(Map.Entry<String, PDFont> eldest) {
427415 }
428416 };
429417
430- private PDFont getCachedFont (String fontName , PDDocument document ) throws IOException {
431- PDFont font = fontCache .get (fontName );
432- if (font == null ) {
433- File fontFile = new File (getFontLocation (fontName ));
434- if (fontFile .exists ()) {
435- font = PDType0Font .load (document , fontFile );
436- } else {
437- font = createPDType1FontFromName (fontName );
438- if (font == null ) {
439- font = new PDType1Font (Standard14Fonts .FontName .HELVETICA );
440- }
441- }
442- fontCache .put (fontName , font );
443- }
444- return font ;
445- }
446-
447418 public void GxAttris (String fontName , int fontSize , boolean fontBold , boolean fontItalic , boolean fontUnderline , boolean fontStrikethru , int Pen , int foreRed , int foreGreen , int foreBlue , int backMode , int backRed , int backGreen , int backBlue ) {
448419 boolean isCJK = false ;
449420 boolean embeedFont = isEmbeddedFont (fontName );
@@ -510,7 +481,8 @@ public void GxAttris(String fontName, int fontSize, boolean fontBold, boolean fo
510481 }
511482 baseFont = createPDType1FontFromName (fontName );
512483 if (baseFont == null ){
513- baseFont = getCachedFont (fontName , document );
484+ baseFont = getOrLoadFont (fontName , document );
485+ baseFontName = fontName ;
514486 }
515487
516488 }
@@ -530,16 +502,22 @@ public void GxAttris(String fontName, int fontSize, boolean fontBold, boolean fo
530502 String fontPath = getFontLocation (fontName );
531503 boolean foundFont = true ;
532504 if (fontPath .equals ("" )) {
533- fontPath = PDFFontDescriptor .getTrueTypeFontLocation (fontName , props );
505+ fontName = fontName .endsWith (style ) ? fontName .substring (0 , fontName .length () - style .length ()) : fontName ;
506+ fontPath = getFontLocation (fontName );
507+ baseFont = getOrLoadFont (fontName , document );
508+ baseFontName = fontName ;
534509 if (fontPath .equals ("" )) {
535510 baseFont = new PDType1Font (Standard14Fonts .FontName .HELVETICA );
511+ baseFontName = "Helvetica" ;
536512 foundFont = false ;
537513 }
538514 }
539515 if (foundFont ){
540516 baseFont = createPDType1FontFromName (fontName );
541- if (baseFont == null )
542- baseFont = getCachedFont (fontName , document );
517+ if (baseFont == null ){
518+ baseFont = getOrLoadFont (fontName , document );
519+ baseFontName = fontName ;
520+ }
543521 }
544522 }
545523 }
@@ -585,8 +563,7 @@ private static PDType1Font createPDType1FontFromName(String fontName) {
585563
586564 public void setAsianFont (String fontName , String style ) {
587565 try {
588- String fontPath = getFontLocation (fontName );
589- baseFont = PDType0Font .load (document , new File (fontPath ));
566+ baseFont = getOrLoadFont (fontName , document );
590567 }
591568 catch (Exception e ) {
592569 log .error ("setAsianFont failed: " , e );
@@ -612,23 +589,19 @@ private PDFont getOrLoadFont(String fontName, PDDocument doc) throws IOException
612589 }
613590
614591 public void GxDrawText (String sTxt , int left , int top , int right , int bottom , int align , int htmlformat , int border , int valign ) {
615- PDPageContentStream cb = null ;
616592 try {
617- cb = currentPageContentStream ;
593+ PDPageContentStream cb = currentPageContentStream ;
618594 boolean printRectangle = false ;
619595 if (props .getBooleanGeneralProperty (Const .BACK_FILL_IN_CONTROLS , true ))
620596 printRectangle = true ;
621597
622598 if (printRectangle && (border == 1 || backFill )) {
623- auxContentStream = cb ;
624- useAuxContentStream = true ;
625599 GxDrawRect (left , top , right , bottom , border , foreColor .getRed (), foreColor .getGreen (), foreColor .getBlue (), backFill ? 1 : 0 , backColor .getRed (), backColor .getGreen (), backColor .getBlue (), 0 , 0 );
626600 }
627601
628602 sTxt = CommonUtil .rtrim (sTxt );
629603
630- String descriptorFontName = baseFont .getFontDescriptor ().getFontName ();
631- PDFont font = getOrLoadFont (descriptorFontName , document );
604+ PDFont font = getOrLoadFont (baseFontName , document );
632605
633606 cb .setFont (font ,fontSize );
634607 cb .setNonStrokingColor (foreColor );
@@ -693,7 +666,6 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value())
693666 GxEndPage ();
694667 GxStartPage ();
695668
696- cb .close ();
697669 cb = currentPageContentStream ;
698670 }
699671 if (this .supportedHTMLTags .contains (element .normalName ()))
@@ -780,6 +752,7 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value())
780752 cb .setNonStrokingColor (backColor );
781753 cb .addRect (rectangle .getLowerLeftX (), rectangle .getLowerLeftY (),rectangle .getWidth (), rectangle .getHeight ());
782754 cb .fill ();
755+ cb .setNonStrokingColor (foreColor );
783756 }
784757
785758 float underlineSeparation = lineHeight / 5 ;
@@ -813,7 +786,7 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value())
813786 contentStream .setNonStrokingColor (foreColor );
814787 contentStream .addRect (underline .getLowerLeftX (), underline .getLowerLeftY (),underline .getWidth (), underline .getHeight ());
815788 contentStream .fill ();
816- contentStream . close ( );
789+ cb . setNonStrokingColor ( foreColor );
817790 }
818791
819792 if (fontStrikethru ) {
@@ -844,7 +817,6 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value())
844817 contentStream .setNonStrokingColor (foreColor );
845818 contentStream .addRect (underline .getLowerLeftX (), underline .getLowerLeftY () - strikethruSeparation * 1 /3 , underline .getWidth (), underline .getHeight ());
846819 contentStream .fill ();
847- contentStream .close ();
848820 }
849821
850822 if (sTxt .trim ().equalsIgnoreCase ("{{Pages}}" )) {
@@ -902,13 +874,6 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value())
902874 }
903875 } catch (Exception ioe ){
904876 log .error ("GxDrawText failed: " , ioe );
905- } finally {
906- try {
907- if (cb != null ) cb .close ();
908- }
909- catch (IOException ioe ) {
910- log .error ("GxDrawText failed to close a content stream to one of it's pages: " , ioe );
911- }
912877 }
913878 }
914879
@@ -1398,8 +1363,6 @@ public void GxEndDocument() {
13981363 }
13991364 }
14001365
1401- private PDPageContentStream currentPageContentStream ;
1402-
14031366 public void GxStartPage () {
14041367 PDPage page = new PDPage (this .pageSize );
14051368 document .addPage (page );
0 commit comments