Hi,
I am using aspose-cells-8.3.2.4 jar.
There is difference between PDF & image of a chart using below code. There is extra white space on right side in chart PDF.
private File chartToImage(Workbook workbook, com.aspose.cells.Chart chart, NameValue[] outputFormatProperties) throws IOException, Exception {
ExcelDataOutputGenerator outputGenerator = excelDataOutputRegistry.getExcelDataOutputGenerator(ExcelDataObjectTypes.CHART);
if (outputGenerator != null) {
try {
File outfile = outputGenerator.getChartImage(chart, outputFormatProperties);
// In case no output is generated by the OutputGenerator, generate the output using Aspose
if (outfile != null) {
return outfile;
}
} catch (Exception e) {
logger.error(“Error thrown by output generator”, e);
}
}
if(isOutputPdf(outputFormatProperties)){
return chartToPdf(workbook, chart, outputFormatProperties);
}
else{
String fileExtension = getImageFileExtension(outputFormatProperties);
File outputFile = OfficeResourceUtility.createTemporaryFile("." + fileExtension);
chart.toImage(outputFile.getAbsolutePath(),
getImageOrPrintOptions(ExcelDataObjectTypes.CHART, outputFormatProperties, true, false));
return outputFile;
}
}
private File chartToPdf(Workbook workbook, com.aspose.cells.Chart chart, NameValue[] outputFormatProperties) throws IOException,Exception {
WorksheetCollection ws = workbook.getWorksheets();
ChartShape cshape = chart.getChartObject();
int sheetIndex = ws.add();
com.aspose.cells.Worksheet worksheet = ws.get(sheetIndex);
//Setting the name of the newly added worksheet
String transientSheetName = “TransientWorksheet”;
worksheet.setName(transientSheetName);
//Copy the Chart to Second Worksheet
worksheet.getShapes().addCopy(cshape, 0, 0, 0, 0);
//Get the new chart and set its height and width accordingly
com.aspose.cells.Chart chart1 = worksheet.getCharts().get(0);
chart1.getChartArea().setHeight(cshape.getHeight());
chart1.getChartObject().setWidth(cshape.getWidth());
//Make remaining worksheets invisible so that they are not part of the output pdf
for (int i = 0; i < ws.getCount(); i++) {
String sheetName = ws.get(i).getName();
if(!transientSheetName.equalsIgnoreCase(sheetName)){
ws.get(i).setVisible(false);
}
}
PdfSaveOptions pdfSaveOptions = getPdfSaveOptions();
worksheet.getPageSetup().setTopMargin(0);
worksheet.getPageSetup().setBottomMargin(0);
worksheet.getPageSetup().setRightMargin(0);
worksheet.getPageSetup().setLeftMargin(0);
// Save the workbook
File outputPdfFile = OfficeResourceUtility.createTemporaryFile(".pdf");
workbook.save(outputPdfFile.getAbsolutePath(), pdfSaveOptions);
return outputPdfFile;
}
Could you please validate this behaviour. Ideally the outputs shoud be same as chart.
I have tested with chart 1 on sheet: Bar and line
Thanks,
Jaspreet