Corrected code for page number in pdf
This commit is contained in:
@@ -36,6 +36,8 @@ import com.itextpdf.layout.element.Cell;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -66,10 +68,10 @@ public class PdfDao {
|
||||
|
||||
Document document = new Document(PageSize.A4, leftMargin, 36f, 50f, 35);
|
||||
PdfWriter writer = PdfWriter.getInstance(document, out);
|
||||
CustomPageEvent pageEvent = new CustomPageEvent(call.getName(), 0);
|
||||
writer.setPageEvent(pageEvent);
|
||||
// CustomPageEvent pageEvent = new CustomPageEvent(call.getName(), 0);
|
||||
// writer.setPageEvent(pageEvent);
|
||||
document.open();
|
||||
pageEvent.setTotalPages(writer.getPageNumber());
|
||||
// pageEvent.setTotalPages(writer.getPageNumber());
|
||||
addLogo(document, "https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/logo.jpg"); // Add your image path here
|
||||
|
||||
|
||||
@@ -91,7 +93,7 @@ public class PdfDao {
|
||||
document.add(new Paragraph(" "));
|
||||
|
||||
// Application ID section (Centered)
|
||||
pageEvent.setTotalPages(writer.getPageNumber());
|
||||
// pageEvent.setTotalPages(writer.getPageNumber());
|
||||
Paragraph appId = new Paragraph("ID domanda :" +"XX00");
|
||||
appId.setAlignment(Element.ALIGN_RIGHT);
|
||||
document.add(appId);
|
||||
@@ -138,7 +140,7 @@ public class PdfDao {
|
||||
Integer pages=0;
|
||||
pages=addLabelValuePair(writer,document, label, value, labelFont,valueFont,call.getName(),pages);
|
||||
if(pages !=0 ){
|
||||
pageEvent.setTotalPages(writer.getPageNumber());
|
||||
// pageEvent.setTotalPages(writer.getPageNumber());
|
||||
}
|
||||
}
|
||||
addColoredLines(writer,document,greenColor);
|
||||
@@ -150,8 +152,9 @@ public class PdfDao {
|
||||
// Adding the "Documenti Allegati" section title
|
||||
document.add(new Paragraph(" "));
|
||||
|
||||
pageEvent.setTotalPages(writer.getPageNumber());
|
||||
// pageEvent.setTotalPages(writer.getPageNumber());
|
||||
document.newPage();
|
||||
// pageEvent.setTotalPages(writer.getPageNumber());
|
||||
document.add(new Paragraph("Documenti Allegati", sectionFont));
|
||||
document.add(new Paragraph(" "));
|
||||
|
||||
@@ -215,11 +218,20 @@ public class PdfDao {
|
||||
|
||||
addColoredLines(writer,document,greenColor);
|
||||
|
||||
// System.out.println(writer.getPageSize());
|
||||
// System.out.println(document.getPageSize());
|
||||
// System.out.println(document.getPageNumber());
|
||||
// System.out.println(writer.getPageNumber());
|
||||
// document.setPageCount(100);
|
||||
// document.setPageCount(writer.getPageNumber());
|
||||
// System.out.println(document.getPageNumber());
|
||||
|
||||
// Close the document
|
||||
document.close();
|
||||
|
||||
// Convert to byte array for response
|
||||
byte[] pdfBytes = out.toByteArray();
|
||||
byte[] pdfBytes =PdfPageNumberInserter.addPageNumbers(out.toByteArray());
|
||||
|
||||
return pdfBytes;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -308,7 +320,7 @@ public class PdfDao {
|
||||
String description = (String) entry.get(keys.get(1)); // Second key's value
|
||||
|
||||
// Store the combined result as a value in the map, with a suitable key
|
||||
String combinedValue = number + ", " + description + ", " + amount; // Concatenate them as a single value
|
||||
String combinedValue = number + "; " + description + "; " + amount; // Concatenate them as a single value
|
||||
extractedMap.put("combined", combinedValue); // Store as a single entry, key as "combined"
|
||||
}
|
||||
|
||||
@@ -380,14 +392,14 @@ public class PdfDao {
|
||||
}
|
||||
|
||||
// Split the value for "combined" into separate parts
|
||||
String[] combinedValues = value.split(", ");
|
||||
String[] combinedValues = value.split("; ");
|
||||
|
||||
// Check if we have 3 parts (number, description, amount)
|
||||
String number = combinedValues[0].trim(); // 1st part (number)
|
||||
String description = combinedValues[1].trim(); // 2nd part (description)
|
||||
String number = combinedValues[0]; // 1st part (number)
|
||||
String description = combinedValues[1]; // 2nd part (description)
|
||||
String amount = "";
|
||||
if (combinedValues.length == 3) {
|
||||
amount = combinedValues[2].trim(); // 3rd part (amount)
|
||||
amount = combinedValues[2]; // 3rd part (amount)
|
||||
}
|
||||
|
||||
// Create PDF cells using the split values
|
||||
@@ -401,9 +413,14 @@ public class PdfDao {
|
||||
cellAmount.setBackgroundColor(new BaseColor(239, 243, 248));
|
||||
|
||||
// Set cell height and add rounded borders
|
||||
cellNumber.setFixedHeight(rowHeight);
|
||||
cellDescription.setFixedHeight(rowHeight);
|
||||
cellAmount.setFixedHeight(rowHeight);
|
||||
// cellNumber.setFixedHeight(rowHeight);
|
||||
// cellDescription.setFixedHeight(rowHeight);
|
||||
// cellAmount.setFixedHeight(rowHeight);
|
||||
|
||||
cellNumber.setMinimumHeight(20f); // Set minimum height for better appearance
|
||||
cellDescription.setMinimumHeight(20f); // Set minimum height for better appearance
|
||||
cellAmount.setMinimumHeight(20f); // Set minimum height for better appearance
|
||||
|
||||
cellNumber.setPadding(7f);
|
||||
cellDescription.setPadding(7f);
|
||||
cellAmount.setPadding(7f);
|
||||
@@ -441,6 +458,8 @@ public class PdfDao {
|
||||
cellKey.setBackgroundColor(new BaseColor(239, 243, 248)); // Light blue for other rows
|
||||
cellValue.setBackgroundColor(new BaseColor(239, 243, 248));
|
||||
|
||||
cellKey.setPadding(7f);
|
||||
cellValue.setPadding(7f);
|
||||
// Set cell height and add rounded borders
|
||||
cellKey.setFixedHeight(rowHeight);
|
||||
cellValue.setFixedHeight(rowHeight);
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
import com.itextpdf.text.BaseColor;
|
||||
import com.itextpdf.text.DocumentException;
|
||||
import com.itextpdf.text.Font;
|
||||
import com.itextpdf.text.FontFactory;
|
||||
import com.itextpdf.text.pdf.BaseFont;
|
||||
import com.itextpdf.text.pdf.PdfReader;
|
||||
import com.itextpdf.text.pdf.PdfStamper;
|
||||
import com.itextpdf.text.pdf.PdfContentByte;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class PdfPageNumberInserter {
|
||||
|
||||
public static byte[] addPageNumbers(byte[] pdfData) throws IOException, DocumentException {
|
||||
PdfReader reader = new PdfReader(pdfData); // Read the generated PDF
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
BaseColor darkGreenColor = new BaseColor(1, 50, 32); // Adjust RGB values as needed
|
||||
Font baseFont = FontFactory.getFont(FontFactory.HELVETICA, 4,darkGreenColor);
|
||||
BaseFont font = baseFont.getBaseFont();
|
||||
|
||||
// PdfStamper allows us to modify the existing PDF
|
||||
PdfStamper stamper = new PdfStamper(reader, outputStream);
|
||||
int totalPages = reader.getNumberOfPages();
|
||||
|
||||
// Set the font for page numbers
|
||||
|
||||
for (int i = 1; i <= totalPages; i++) {
|
||||
// Get the content of the current page
|
||||
PdfContentByte over = stamper.getOverContent(i);
|
||||
over.beginText();
|
||||
over.setFontAndSize(font, 12);
|
||||
|
||||
// Add the page number at the bottom center of the page
|
||||
over.showTextAligned(PdfContentByte.ALIGN_CENTER, "Page " + i + " of " + totalPages, 300, 30, 0);
|
||||
|
||||
over.endText();
|
||||
}
|
||||
|
||||
stamper.close();
|
||||
reader.close();
|
||||
|
||||
return outputStream.toByteArray(); // Return the modified PDF with page numbers
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user