Hi Aspose Cells team,
- Create a workbook where chart data labels reference cells using formulas.
- Save as XLSX: ok - the data labels are rendered with calculated formulas results
- Save as PDF: not ok - data labels are rendered with original series value
Here is the code snippet:
String inputFilePath = "/input.xlsx";
String outputExcelFilePath = "/output.xlsx";
String outputPdfFilePath = "/output.pdf";
Workbook workBook = null;
PdfSaveOptions saveOptions = new PdfSaveOptions(SaveFormat.PDF);
saveOptions.setCreatedTime(DateTime.getNow());
saveOptions.setAllColumnsInOnePagePerSheet(true);
saveOptions.setOnePagePerSheet(true);
saveOptions.setCalculateFormula(true);
// Set formulas
workBook = new Workbook(inputFilePath);
Worksheet sheet = workBook.getWorksheets().get(0);
sheet.getCells().get("A2").setValue("123");
sheet.getCells().get("B2").setFormula("=B1");
sheet.getCells().get("C2").setFormula("=C1*10");
// Set data labels based on formulas
Chart chart = sheet.getCharts().get(0);
for (int i = 0; i < chart.getNSeries().get(0).getPoints().getCount(); i++)
{
int row = sheet.getCells().get("A2").getRow();
int col = sheet.getCells().get("A2").getColumn()+i;
String cellName = sheet.getCells().get(row, col).getName();
ChartPoint point = chart.getNSeries().get(0).getPoints().get(i);
String formula = "="+sheet.getName()+"!"+cellName;
log.debug("formula="+formula);
point.getDataLabels().setLinkedSource(formula);
}
// Save as Excel (will have expected data labels)
workBook.save(outputExcelFilePath);
// Save as PDF - will not have the expected data labels
workBook.save(outputPdfFilePath, saveOptions);
// Workaround: reopen Workbook and the save as PDF - have correct data labels
// workBook = new Workbook(outputExcelFilePath);
// workBook.save(outputPdfFilePath, saveOptions);
Regards,
Jean.
input.zip (11.3 KB)
expected_ok.pdf (28.3 KB)
expected_nok.pdf (28.3 KB)