@@ -861,7 +861,7 @@ else if (barcodeType != null) {
861
861
templateY = this .pageSize .getUpperRightY () - bottomAux - topMargin - bottomMargin ;
862
862
templateCreated = true ;
863
863
}
864
- sTxt = String . valueOf ( this . page ) ;
864
+ sTxt = "{{pages}}" ;
865
865
}
866
866
867
867
float textBlockWidth = rightAux - leftAux ;
@@ -1265,12 +1265,86 @@ else if (length == 15840 && width == 12240)
1265
1265
return new PDRectangle ((int )(width / PAGE_SCALE_X ) + leftMargin , (int )(length / PAGE_SCALE_Y ) + topMargin );
1266
1266
}
1267
1267
1268
+ private void replaceTemplatePages () throws IOException {
1269
+ int totalPages = document .getNumberOfPages ();
1270
+ for (int i = 0 ; i < totalPages ; i ++) {
1271
+ final org .apache .pdfbox .pdmodel .PDPage page = document .getPage (i );
1272
+ final java .util .List <float []> replacements = new java .util .ArrayList <>();
1273
+ org .apache .pdfbox .text .PDFTextStripper stripper = new org .apache .pdfbox .text .PDFTextStripper () {
1274
+ @ Override
1275
+ protected void writeString (String text , java .util .List <org .apache .pdfbox .text .TextPosition > textPositions ) throws IOException {
1276
+ String placeholder = "{{pages}}" ;
1277
+ int index = text .indexOf (placeholder );
1278
+ while (index != -1 && index + placeholder .length () <= textPositions .size ()) {
1279
+ float minX = Float .MAX_VALUE ;
1280
+ float maxX = 0 ;
1281
+ float minY = Float .MAX_VALUE ;
1282
+ float maxY = 0 ;
1283
+ for (int j = index ; j < index + placeholder .length (); j ++) {
1284
+ org .apache .pdfbox .text .TextPosition tp = textPositions .get (j );
1285
+ float tpX = tp .getXDirAdj ();
1286
+ float tpY = tp .getYDirAdj ();
1287
+ float tpWidth = tp .getWidthDirAdj ();
1288
+ float tpHeight = tp .getHeightDir ();
1289
+ if (tpX < minX ) {
1290
+ minX = tpX ;
1291
+ }
1292
+ if (tpX + tpWidth > maxX ) {
1293
+ maxX = tpX + tpWidth ;
1294
+ }
1295
+ if (tpY < minY ) {
1296
+ minY = tpY ;
1297
+ }
1298
+ if (tpY + tpHeight > maxY ) {
1299
+ maxY = tpY + tpHeight ;
1300
+ }
1301
+ }
1302
+ float bboxWidth = maxX - minX ;
1303
+ float bboxHeight = maxY - minY ;
1304
+ float origBoxBottom = pageSize .getHeight () - maxY ;
1305
+ float originalCenterX = minX + bboxWidth / 2 ;
1306
+ float originalCenterY = origBoxBottom + bboxHeight / 2 ;
1307
+ float newCenterY = originalCenterY + (bboxHeight * 0.5f );
1308
+ float enlargedWidth = bboxWidth * 2.5f ;
1309
+ float enlargedHeight = bboxHeight * 2.5f ;
1310
+ float rectX = originalCenterX - (enlargedWidth / 2 );
1311
+ float rectY = newCenterY - (enlargedHeight / 2 );
1312
+ float baselineY = newCenterY ;
1313
+ replacements .add (new float [] { rectX , rectY , enlargedWidth , enlargedHeight , baselineY });
1314
+ index = text .indexOf (placeholder , index + placeholder .length ());
1315
+ }
1316
+ super .writeString (text , textPositions );
1317
+ }
1318
+ };
1319
+ stripper .setStartPage (i + 1 );
1320
+ stripper .setEndPage (i + 1 );
1321
+ stripper .getText (document );
1322
+ if (!replacements .isEmpty ()) {
1323
+ try (org .apache .pdfbox .pdmodel .PDPageContentStream cs = new org .apache .pdfbox .pdmodel .PDPageContentStream (
1324
+ document , page , org .apache .pdfbox .pdmodel .PDPageContentStream .AppendMode .APPEND , true , true )) {
1325
+ for (float [] rep : replacements ) {
1326
+ cs .addRect (rep [0 ], rep [1 ], rep [2 ], rep [3 ]);
1327
+ cs .setNonStrokingColor (java .awt .Color .WHITE );
1328
+ cs .fill ();
1329
+ cs .beginText ();
1330
+ cs .setFont (templateFont , templateFontSize );
1331
+ cs .setNonStrokingColor (templateColorFill );
1332
+ cs .newLineAtOffset (rep [0 ], rep [4 ]);
1333
+ cs .showText (String .valueOf (totalPages ));
1334
+ cs .endText ();
1335
+ }
1336
+ }
1337
+ }
1338
+ }
1339
+ }
1340
+
1268
1341
public void GxEndDocument () {
1269
1342
try {
1270
1343
if (document .getNumberOfPages () == 0 ) {
1271
1344
document .addPage (new PDPage (this .pageSize ));
1272
1345
pages ++;
1273
1346
}
1347
+ replaceTemplatePages ();
1274
1348
int copies = 1 ;
1275
1349
try {
1276
1350
copies = Integer .parseInt (printerSettings .getProperty (form , Const .COPIES ));
0 commit comments