Hi team,
Thanks for your patience and replies.
arena.zip (103.6 KB)
are the files I used.
I tried to add textbox in the last column of the excel sheet. I wanted to make each textbox the size of the left one for each row. I used the following code -
public static void convertToPdf(ByteArrayInputStream excelInputStream) throws Exception {
ByteArrayOutputStream outputStreamPdf = new ByteArrayOutputStream();
try {
log.info("Initiating Excel to pdf conversion");
Workbook workbook = new Workbook(excelInputStream);
// Expanding columns before merging
WorksheetCollection worksheets = workbook.getWorksheets();
int workSheetCount = worksheets.getCount();
for (int sheetNo = 0; sheetNo < workSheetCount; sheetNo++) {
Worksheet sheet = worksheets.get(sheetNo);
var lastRow = sheet.getCells().getMaxDataRow();
var lastCol = sheet.getCells().getMaxDataColumn();
Cells cells = sheet.getCells();
for(int i=7;i<=lastRow;i++) {
Cell cell = cells.get(i, lastCol);
Cell leftCell = cells.get(i, lastCol-1);
System.out.println("adding textbox at (" + i + ", " + lastCol + ")");
System.out.println("height: " + leftCell.getHeightOfValue() + " width: " + leftCell.getWidthOfValue());
int textBoxIndex = sheet.getTextBoxes().add(i, lastCol, leftCell.getHeightOfValue(), leftCell.getWidthOfValue());
TextBox textbox = sheet.getTextBoxes().get(textBoxIndex);
textbox.setText("Row: " + i + " Col: " + lastCol);
textbox.getTextBody().getTextAlignment().setAutoSize(true);
textbox.setPlacement(PlacementType.FREE_FLOATING);
textbox.getFont().setColor(Color.getBlue());
FillFormat fillFormat = textbox.getFill();
fillFormat.getSolidFill().setColor(Color.getAquamarine());
}
// cosmetics
PageSetup pageSetup = sheet.getPageSetup();
pageSetup.setPrintArea("");
pageSetup.setPrintGridlines(true);
sheet.autoFitColumns();
}
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setOnePagePerSheet(true);
saveOptions.setAllColumnsInOnePagePerSheet(true);
workbook.save("C:/Users/shauryac/arena/output.pdf", saveOptions);
log.info("Conversion from Excel to pdf completed");
} catch (Exception ex) {
log.info("Exception occurred while converting Excel to pdf : ", ex);
throw ex;
} finally {
outputStreamPdf.close();
}
}
There are some problems that I now see -
- There are new columns added to the right.
- The textboxes added are at least 2-3 columns right of where I want them to be.
- In the final converted pdf file, I’m unable to fill the added textboxes.
The requirement is to have a fillable pdf in the end.
Please help
Thanks,
Shaurya