Skip to content

Commit 4019209

Browse files
authored
{{Pages}} template was not working when PDFBox library is used (#901)
Issue: 201569
1 parent c8ad69b commit 4019209

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

java/src/main/java/com/genexus/reports/PDFReportPDFBox.java

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public class PDFReportPDFBox extends GXReportPDFCommons{
5353
private String barcodeType = null;
5454
private PDDocument document;
5555
private PDDocumentCatalog writer;
56-
private PDPageContentStream template;
57-
private PDFormXObject formXObjecttemplate;
5856
private PDType0Font templateFont;
57+
private float templateX;
58+
private float templateY;
5959
public boolean lineCapProjectingSquare = true;
6060
public boolean barcode128AsImage = true;
6161
ConcurrentHashMap<String, PDImageXObject> documentImages;
@@ -813,20 +813,13 @@ else if (valign == PDFReportPDFBox.VerticalAlign.BOTTOM.value())
813813

814814
if(sTxt.trim().equalsIgnoreCase("{{Pages}}")) {
815815
if (!templateCreated) {
816-
formXObjecttemplate = new PDFormXObject(document);
817-
template = new PDPageContentStream(document, formXObjecttemplate, outputStream);
818-
formXObjecttemplate.setResources(new PDResources());
819-
formXObjecttemplate.setBBox(new PDRectangle(right - left, bottom - top));
816+
templateFont = new PDType0Font(baseFont.getCOSObject());
817+
templateFontSize = fontSize;
818+
templateColorFill = foreColor;
819+
templateX = leftAux + leftMargin;
820+
templateY = this.pageSize.getUpperRightY() - bottomAux - topMargin -bottomMargin;
820821
templateCreated = true;
821822
}
822-
PDFormXObject form = new PDFormXObject(document);
823-
PDPageContentStream contentStream = getNewPDPageContentStream();
824-
contentStream.transform(Matrix.getTranslateInstance(leftAux + leftMargin, leftAux + leftMargin));
825-
contentStream.drawForm(form);
826-
contentStream.close();
827-
templateFont = new PDType0Font(baseFont.getCOSObject());
828-
templateFontSize = fontSize;
829-
templateColorFill = foreColor;
830823
return;
831824
}
832825

@@ -1240,23 +1233,18 @@ public void GxEndDocument() {
12401233
document.addPage(new PDPage(this.pageSize));
12411234
pages++;
12421235
}
1243-
if (template != null) {
1244-
try{
1245-
template.beginText();
1246-
template.setFont(baseFont, fontSize);
1247-
template.setTextMatrix(new Matrix());
1248-
template.setNonStrokingColor(templateColorFill);
1249-
template.showText(String.valueOf(pages));
1250-
template.endText();
1251-
template.close();
1252-
for (PDPage page : document.getPages()){
1253-
try (PDPageContentStream templatePainter = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND,false)) {
1254-
templatePainter.drawForm(formXObjecttemplate);
1255-
}
1236+
if (templateCreated) {
1237+
for (PDPage page : document.getPages()){
1238+
try (PDPageContentStream templatePainter = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND,true, true)) {
1239+
templatePainter.beginText();
1240+
templatePainter.setFont(templateFont, templateFontSize);
1241+
templatePainter.setNonStrokingColor(templateColorFill);
1242+
templatePainter.newLineAtOffset(templateX, templateY);
1243+
templatePainter.showText(String.valueOf(pages));
1244+
templatePainter.endText();
1245+
} catch (IOException e){
1246+
log.error("GxEndDocument: failed to apply template" , e);;
12561247
}
1257-
template.close();
1258-
} catch (IOException e){
1259-
log.error("GxEndDocument: failed to apply template" , e);;
12601248
}
12611249
}
12621250
int copies = 1;

0 commit comments

Comments
 (0)