Updated code for pdf correction
This commit is contained in:
@@ -17,6 +17,7 @@ import net.gepafin.tendermanagement.model.response.*;
|
||||
import net.gepafin.tendermanagement.service.CallService;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.util.Validator;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -25,6 +26,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -60,7 +62,7 @@ public class PdfDao {
|
||||
// writer.setPageEvent(pageEvent);
|
||||
document.open();
|
||||
// pageEvent.setTotalPages(writer.getPageNumber());
|
||||
addLogo(document, "https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/logo.jpg"); // Add your image path here
|
||||
addLogo(document, "logo.jpg"); // Add your image path here
|
||||
|
||||
|
||||
BaseColor customColor = new BaseColor(0, 128, 0); // Adjust RGB values as needed
|
||||
@@ -82,8 +84,6 @@ public class PdfDao {
|
||||
|
||||
ApplicationGetResponseBean applicationGetResponseBean=applicationDao.getApplicationByFormId(request, applicationId, null);
|
||||
for(FormApplicationResponse formApplicationResponse: applicationGetResponseBean.getForm()) {
|
||||
document.add(new Paragraph(formApplicationResponse.getLabel(),sectionFont));
|
||||
document.add(new Paragraph(" ")); // Add line break
|
||||
List<FieldLabelValuePairRequest> fieldLabelValuePairRequests = getFormFieldsToLabels(formApplicationResponse,writer,document);
|
||||
addColoredLines(writer,document,greenColor);
|
||||
document.add(new Paragraph(" ")); // Add line break
|
||||
@@ -231,24 +231,70 @@ public class PdfDao {
|
||||
}
|
||||
}
|
||||
else {
|
||||
PdfPCell valueCell = new PdfPCell(new Phrase(String.valueOf(value), valueFont));
|
||||
valueCell.setPadding(5f); // Increase padding for better spacing
|
||||
valueCell.setPaddingLeft(leftMargin); // Increase left margin for value
|
||||
valueCell.setBorder(Rectangle.NO_BORDER); // Remove border for value cell
|
||||
valueCell.setMinimumHeight(30f);
|
||||
valueCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
valueCell.setCellEvent(new RoundedCorners()); // Apply rounded corners
|
||||
|
||||
valueTable.addCell(valueCell);
|
||||
document.add(valueTable);
|
||||
String fieldValue=Utils.convertToString(value);
|
||||
Image img = null; // This may throw MalformedURLException
|
||||
if (fieldValue.equalsIgnoreCase("true")) {
|
||||
// Use images for tick and cross
|
||||
try {
|
||||
img = Image.getInstance("true.jpg");
|
||||
} catch (IOException e) {
|
||||
log.error("Error while uploading image for pdf for true");
|
||||
}
|
||||
img.scaleAbsolute(15, 15); // Resize the image if needed
|
||||
|
||||
PdfPCell cell = new PdfPCell(img);
|
||||
cell.setPadding(5f); // Increase padding for better spacing
|
||||
cell.setPaddingLeft(leftMargin); // Increase left margin for value
|
||||
cell.setBorder(Rectangle.NO_BORDER); // Remove border for value cell
|
||||
cell.setMinimumHeight(30f);
|
||||
cell.setVerticalAlignment(Element.ALIGN_LEFT);
|
||||
cell.setCellEvent(new RoundedCorners()); // Apply rounded corners
|
||||
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
||||
valueTable.addCell(cell);
|
||||
document.add(valueTable);
|
||||
}
|
||||
else if (fieldValue.equalsIgnoreCase("false")) {
|
||||
// Use images for tick and cross
|
||||
try {
|
||||
img = Image.getInstance("false.jpg");
|
||||
} catch (IOException e) {
|
||||
log.error("Error while uploading image for pdf for true");
|
||||
}
|
||||
img.scaleAbsolute(15, 15); // Resize the image if needed
|
||||
|
||||
PdfPCell cell = new PdfPCell(img);
|
||||
cell.setPadding(5f); // Increase padding for better spacing
|
||||
cell.setPaddingLeft(leftMargin); // Increase left margin for value
|
||||
cell.setBorder(Rectangle.NO_BORDER); // Remove border for value cell
|
||||
cell.setMinimumHeight(30f);
|
||||
cell.setVerticalAlignment(Element.ALIGN_LEFT);
|
||||
cell.setCellEvent(new RoundedCorners()); // Apply rounded corners
|
||||
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
||||
valueTable.addCell(cell);
|
||||
document.add(valueTable);
|
||||
}
|
||||
else {
|
||||
PdfPCell valueCell = new PdfPCell(new Phrase(String.valueOf(value), valueFont));
|
||||
valueCell.setPadding(5f); // Increase padding for better spacing
|
||||
valueCell.setPaddingLeft(leftMargin); // Increase left margin for value
|
||||
valueCell.setBorder(Rectangle.NO_BORDER); // Remove border for value cell
|
||||
valueCell.setMinimumHeight(30f);
|
||||
valueCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
valueCell.setCellEvent(new RoundedCorners()); // Apply rounded corners
|
||||
|
||||
valueTable.addCell(valueCell);
|
||||
document.add(valueTable);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
document.add(new Paragraph("\n")); // Add line break after each value
|
||||
}
|
||||
|
||||
private Document createPdfTable(List<Map<String, Object>> extractedData, Document document, ContentResponseBean contentResponseBean) throws DocumentException {
|
||||
// Create a PdfPTable with dynamic column count based on stateFieldMap size
|
||||
Map<String, String> stateFieldMap = new HashMap<>();
|
||||
Map<String, Boolean> stateFieldBoolean = new HashMap<>();
|
||||
|
||||
// Populate stateFieldMap from contentResponseBean settings
|
||||
contentResponseBean.getSettings().stream()
|
||||
@@ -268,8 +314,23 @@ public class PdfDao {
|
||||
stateFieldMap.put(fieldName, fieldDataValue);
|
||||
}
|
||||
});
|
||||
|
||||
PdfPTable table = new PdfPTable(stateFieldMap.size()); // Number of columns equals the number of map entries
|
||||
contentResponseBean.getSettings().stream()
|
||||
.filter(setting -> "table_columns".equals(setting.getName())) // Check for "table_columns"
|
||||
.map(SettingResponseBean::getValue)
|
||||
.filter(Objects::nonNull) // Ensure value is not null
|
||||
.filter(settingValue -> settingValue instanceof Map) // Ensure value is a Map
|
||||
.map(settingValue -> (Map<String, Object>) settingValue) // Cast to Map
|
||||
.map(valueMap -> (List<Map<String, Object>>) valueMap.get("stateFieldData")) // Extract stateFieldData list
|
||||
.filter(Objects::nonNull) // Ensure stateFieldData is not null
|
||||
.flatMap(List::stream) // Flatten the list of field data maps
|
||||
.forEach(fieldData -> {
|
||||
String fieldName = (String) fieldData.get("name"); // Get the name field
|
||||
Boolean predefined = (Boolean) fieldData.get("predefined"); // Get the predefined field
|
||||
if (fieldName != null && fieldName != null) {
|
||||
stateFieldBoolean.put(fieldName, predefined);
|
||||
}
|
||||
});
|
||||
PdfPTable table = new PdfPTable(stateFieldMap.size()); // Number of columns equals the number of map entries
|
||||
table.setWidthPercentage(100); // Set table width to 100%
|
||||
table.setTableEvent(new RoundedBorderEvent());
|
||||
|
||||
@@ -278,29 +339,37 @@ public class PdfDao {
|
||||
float maxTableHeight = 700f; // Maximum height of the table before a page break
|
||||
boolean headersAdded = false; // Flag to check if headers have been added
|
||||
|
||||
List<String> trueKeys = new ArrayList<>();
|
||||
List<String> falseKeys = new ArrayList<>();
|
||||
for (Map.Entry<String, Boolean> entry : stateFieldBoolean.entrySet()) {
|
||||
if (Boolean.TRUE.equals(entry.getValue())) {
|
||||
trueKeys.add(entry.getKey()); // Store true keys
|
||||
} else {
|
||||
falseKeys.add(entry.getKey()); // Store false keys
|
||||
}
|
||||
}
|
||||
List<String> orderedKeys = new ArrayList<>(trueKeys);
|
||||
orderedKeys.addAll(falseKeys);
|
||||
// Iterate through extracted data to populate the table
|
||||
for (Map<String, Object> row : extractedData) {
|
||||
// Add headers once
|
||||
if (!headersAdded) {
|
||||
for (Map.Entry<String, String> stateField : stateFieldMap.entrySet()) {
|
||||
String headerValue = stateField.getValue(); // Header text
|
||||
for (String key : orderedKeys) {
|
||||
String headerValue = stateFieldMap.get(key); // Header text
|
||||
PdfPCell headerCell = new PdfPCell(new Phrase(headerValue)); // Create a new PdfPCell for the header
|
||||
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER); // Center align
|
||||
headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
headerCell.setBackgroundColor(new BaseColor(178, 190, 181)); // Light gray background for header
|
||||
|
||||
PdfPCell headerCell = new PdfPCell(new Phrase(headerValue)); // Create a new PdfPCell for the header
|
||||
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER); // Center align
|
||||
headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
headerCell.setBackgroundColor(new BaseColor(178, 190, 181)); // Light gray background for header
|
||||
|
||||
table.addCell(headerCell); // Add the header cell to the table
|
||||
}
|
||||
table.addCell(headerCell); // Add the header cell to the table
|
||||
}
|
||||
headersAdded = true; // Prevent headers from being added again
|
||||
}
|
||||
|
||||
// Add data rows matching stateFieldMap keys
|
||||
for (Map.Entry<String, String> stateField : stateFieldMap.entrySet()) {
|
||||
String stateKey = stateField.getKey(); // Get the key from stateFieldMap
|
||||
if (row.containsKey(stateKey)) { // If row contains the stateKey
|
||||
Object value = row.get(stateKey); // Get the value from the row map
|
||||
|
||||
for (String key : orderedKeys) { // Iterate over the ordered keys
|
||||
Object value = row.getOrDefault(key, ""); // Fetch value or use empty string if key not present
|
||||
PdfPCell dynamicCell = new PdfPCell(new Phrase(value != null ? value.toString() : "", textFont));
|
||||
dynamicCell.setBackgroundColor(new BaseColor(239, 243, 248)); // Light blue for the cell
|
||||
dynamicCell.setMinimumHeight(rowHeight);
|
||||
|
||||
Reference in New Issue
Block a user