Transparency not preserved in the PDF output of a chart

Hi,

I am using Aspose.Cells v8.5.2.4
In case of Chart 1 to PDF conversion the transparency is not preserved in the output.

Here’s the sample code for Chart to PDF conversion:

private static void chartToPDF() throws Exception {
Workbook workbook = new Workbook(“C:\Aspose Issues\80067\Excel.xlsx”);
WorksheetCollection ws = workbook.getWorksheets();
Chart chart = workbook.getWorksheets().get(0).getCharts().get(“Chart 1”);
ChartShape cshape = chart.getChartObject();
int sheetIndex = ws.add();
com.aspose.cells.Worksheet worksheet = ws.get(sheetIndex);
chart.calculate();
double chartWidthInInches = chart.getChartObject().getWidthInch();
double chartHeightInInches = chart.getChartObject().getHeightInch();
boolean defaultApproach = true;
if (chartWidthInInches > 18 || chartHeightInInches > 5) {
// The approach being followed here is to iterate as many columns as colwidth in pixels & set each column width equal = 1 pixel.
// Similarly for chart height, iterate through as many rows as row height & set each row height = 1 pixel.
// This is being done to fit in large charts that have height & width beyond maximum row/col dimensions & white spaces around
// the chart.

double chartWidthInPixels = chart.getChartObject().getWidth();
double chartHeightInPixels = chart.getChartObject().getHeight();
// Deliberately including one more column because Aspose considers the last column adjacent to chart which leads to white space
// on right of chart
for (int i = 0; i <= chartWidthInPixels; i++) {
worksheet.getCells().setColumnWidthPixel(i, 1);
}

// Deliberately including one more row because Aspose considers the last row adjacent to chart which leads to white space
// below the chart.
for (int i = 0; i <= chartHeightInPixels; i++) {
worksheet.getCells().setRowHeightPixel(i, 1);
}
defaultApproach = false;
} else {
worksheet.getCells().setColumnWidthInch(0, chartWidthInInches);
worksheet.getCells().setRowHeightInch(0, chartHeightInInches);
}

//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.getChartObject().setHeightInch(chartHeightInInches);
chart1.getChartObject().setWidthInch(chartWidthInInches);
//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 = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
worksheet.getPageSetup().setTopMargin(0);
worksheet.getPageSetup().setBottomMargin(0);
worksheet.getPageSetup().setRightMargin(0);
worksheet.getPageSetup().setLeftMargin(0);
//Setting Print area to first cell only as the chart is contained in the first cell (A1:A1)
if (defaultApproach) {
worksheet.getPageSetup().setPrintArea(“A1:A1”);
}
// Save the workbook
workbook.save(“C:\Aspose Issues\80067\out.pdf”, pdfSaveOptions);
}


Also, attached the sample Excel doc & output.

Thanks,
Jaspreet

Hi,

Thanks for your posting and using Aspose.Cells.

We have tested this issue with the latest version: Aspose.Cells
for Java v8.6.0.3
and found transparency is working fine. To test this issue, I added an oval shape behind your transparent chart and then converted the excel file into pdf and oval shape did show up because of chart transparency in the output pdf.

I have attached the source excel file and the output pdf generated with the following code for your reference. Also I have attached the screenshot describing this issue in detail for you to check out.

Java
String filePath = “F:\Shak-Data-RW\Downloads\Excel.xlsx”;

Workbook workbook = new Workbook(filePath);

PdfSaveOptions opts = new PdfSaveOptions();
opts.setImageType(ImageFormat.getPng());
opts.setOnePagePerSheet(true);

workbook.save(filePath + “.out.pdf”, opts);

Hi,

I tried the above options but didn’t work because
we are copying chart to new Worksheet in order to take PDF output of chart.
The Chart copies well to the new worksheet but its background colour does not.Since background colour doesn’t copy to new worksheet thus PDF output shows white background.

Here’s the code snippet & excel documents for reference.

private static void chartToPDF() throws Exception {
Workbook workbook = new Workbook(“C:\Aspose Issues\80067\Excel.xlsx”);
WorksheetCollection ws = workbook.getWorksheets();
Chart chart = workbook.getWorksheets().get(0).getCharts().get(“Chart 1”);
ChartShape cshape = chart.getChartObject();
int sheetIndex = ws.add();
com.aspose.cells.Worksheet worksheet = ws.get(sheetIndex);
chart.calculate();
double chartWidthInInches = chart.getChartObject().getWidthInch();
double chartHeightInInches = chart.getChartObject().getHeightInch();
boolean defaultApproach = true;
if (chartWidthInInches > 18 || chartHeightInInches > 5) {
// The approach being followed here is to iterate as many columns as colwidth in pixels & set each column width equal = 1 pixel.
// Similarly for chart height, iterate through as many rows as row height & set each row height = 1 pixel.
// This is being done to fit in large charts that have height & width beyond maximum row/col dimensions & white spaces around
// the chart.

double chartWidthInPixels = chart.getChartObject().getWidth();
double chartHeightInPixels = chart.getChartObject().getHeight();
// Deliberately including one more column because Aspose considers the last column adjacent to chart which leads to white space
// on right of chart
for (int i = 0; i <= chartWidthInPixels; i++) {
worksheet.getCells().setColumnWidthPixel(i, 1);
}

// Deliberately including one more row because Aspose considers the last row adjacent to chart which leads to white space
// below the chart.
for (int i = 0; i <= chartHeightInPixels; i++) {
worksheet.getCells().setRowHeightPixel(i, 1);
}
defaultApproach = false;
} else {
worksheet.getCells().setColumnWidthInch(0, chartWidthInInches);
worksheet.getCells().setRowHeightInch(0, chartHeightInInches);
}

//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.getChartObject().setHeightInch(chartHeightInInches);
chart1.getChartObject().setWidthInch(chartWidthInInches);
//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 = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
worksheet.getPageSetup().setTopMargin(0);
worksheet.getPageSetup().setBottomMargin(0);
worksheet.getPageSetup().setRightMargin(0);
worksheet.getPageSetup().setLeftMargin(0);
//Setting Print area to first cell only as the chart is contained in the first cell (A1:A1)
if (defaultApproach) {
worksheet.getPageSetup().setPrintArea(“A1:A1”);
}
// Save the workbook
workbook.save(“D:\out.xlsx”);
// workbook.save(“C:\Aspose Issues\80067\out.pdf”, pdfSaveOptions);
}


Can you suggest options to ensure exact replica of Chart to be copied to the new worksheet.

Thanks,
Jaspreet

Hi Jaspreet,


Thank you for clarifying the scenario.

We are able to notice the issue of transparency while copying the chart object to another worksheet. We have logged this incident in our bug tracking system under the ticket CELLSJAVA-41506 for further investigation. Please allow us some time to properly analyze the scenario and get back to you with updates in this regard.

Hello.

At IHS we also have Aspose.cells license and we have encountered this problem too.

Fixing it is important for us, because our users face it in everyday work.

Regards,
Tomek

Hi Tomek,

Please note, the ticket attached to this thread is currently in analysis phase therefore we cannot share an estimated time for fix at the moment. Regarding your scenario, we strongly suggest you to create a new thread with problem description and supporting documents so we could evaluate it separately and log separate ticket, if applicable.

Hi,

Thanks for using Aspose.Cells.

The chart (workbook.getWorksheets().get(0).getCharts().get(“Chart 1”)) that you copied is not transparent. Do you want to copy that chart in “Transparency PDF”?

You may modify the code segment to Chart chart = workbook.getWorksheets().get(“Transparency PDF”).getCharts().get(“Chart 1”).

It will resolve your issue. Let us know your feedback.