From 401920917758bdddfdf0e657543758804245d4f2 Mon Sep 17 00:00:00 2001 From: iroqueta <46004974+iroqueta@users.noreply.github.com> Date: Wed, 6 Nov 2024 09:04:29 -0300 Subject: [PATCH] {{Pages}} template was not working when PDFBox library is used (#901) Issue: 201569 --- .../com/genexus/reports/PDFReportPDFBox.java | 48 +++++++------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/java/src/main/java/com/genexus/reports/PDFReportPDFBox.java b/java/src/main/java/com/genexus/reports/PDFReportPDFBox.java index 30f8d59cd..0522cae00 100644 --- a/java/src/main/java/com/genexus/reports/PDFReportPDFBox.java +++ b/java/src/main/java/com/genexus/reports/PDFReportPDFBox.java @@ -53,9 +53,9 @@ public class PDFReportPDFBox extends GXReportPDFCommons{ private String barcodeType = null; private PDDocument document; private PDDocumentCatalog writer; - private PDPageContentStream template; - private PDFormXObject formXObjecttemplate; private PDType0Font templateFont; + private float templateX; + private float templateY; public boolean lineCapProjectingSquare = true; public boolean barcode128AsImage = true; ConcurrentHashMap documentImages; @@ -813,20 +813,13 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value()) if(sTxt.trim().equalsIgnoreCase("{{Pages}}")) { if (!templateCreated) { - formXObjecttemplate = new PDFormXObject(document); - template = new PDPageContentStream(document, formXObjecttemplate, outputStream); - formXObjecttemplate.setResources(new PDResources()); - formXObjecttemplate.setBBox(new PDRectangle(right - left, bottom - top)); + templateFont = new PDType0Font(baseFont.getCOSObject()); + templateFontSize = fontSize; + templateColorFill = foreColor; + templateX = leftAux + leftMargin; + templateY = this.pageSize.getUpperRightY() - bottomAux - topMargin -bottomMargin; templateCreated = true; } - PDFormXObject form = new PDFormXObject(document); - PDPageContentStream contentStream = getNewPDPageContentStream(); - contentStream.transform(Matrix.getTranslateInstance(leftAux + leftMargin, leftAux + leftMargin)); - contentStream.drawForm(form); - contentStream.close(); - templateFont = new PDType0Font(baseFont.getCOSObject()); - templateFontSize = fontSize; - templateColorFill = foreColor; return; } @@ -1240,23 +1233,18 @@ public void GxEndDocument() { document.addPage(new PDPage(this.pageSize)); pages++; } - if (template != null) { - try{ - template.beginText(); - template.setFont(baseFont, fontSize); - template.setTextMatrix(new Matrix()); - template.setNonStrokingColor(templateColorFill); - template.showText(String.valueOf(pages)); - template.endText(); - template.close(); - for (PDPage page : document.getPages()){ - try (PDPageContentStream templatePainter = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND,false)) { - templatePainter.drawForm(formXObjecttemplate); - } + if (templateCreated) { + for (PDPage page : document.getPages()){ + try (PDPageContentStream templatePainter = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND,true, true)) { + templatePainter.beginText(); + templatePainter.setFont(templateFont, templateFontSize); + templatePainter.setNonStrokingColor(templateColorFill); + templatePainter.newLineAtOffset(templateX, templateY); + templatePainter.showText(String.valueOf(pages)); + templatePainter.endText(); + } catch (IOException e){ + log.error("GxEndDocument: failed to apply template" , e);; } - template.close(); - } catch (IOException e){ - log.error("GxEndDocument: failed to apply template" , e);; } } int copies = 1;