Free Support Forum - aspose.com

Pivot Table Image Capture doesn't retain color and styles

I’m using the Aspose.Cells Java API to capture an image of a Pivot Table in an excel spreadsheet. The resulting image does not retain the color and style of the original pivot table. Why would that happen?

Here is the code I’m using:

            PivotTableCollection pivotTables = worksheet.getPivotTables();
			Iterator<PivotTable> it = pivotTables.iterator();

			while (it.hasNext()) {
				PivotTable pivotTable = it.next();

				System.out.println("doSnapshotByName() :: " + pivotTable.getName());
				CellArea pivotArea = pivotTable.getTableRange1();
				System.out.println("doSnapshotByName() :: pivot table area :: " + pivotArea);
				if (request.getElementName().equalsIgnoreCase(pivotTable.getName())) {
					System.out.println("doSnapshotByName() :: found pivot table :: " + pivotTable.getName());
					worksheet.getPageSetup().setPrintArea(buildPrintArea(pivotArea));
					worksheet.getPageSetup().setLeftMargin(0.0);
					worksheet.getPageSetup().setRightMargin(0.0);
					worksheet.getPageSetup().setTopMargin(0.0);
					worksheet.getPageSetup().setBottomMargin(0.0);
					
					if (request.getImageOutputFilePath().contains(".emf"))
						options.setImageFormat(ImageFormat.getEmf());
					else
						options.setImageFormat(ImageFormat.getPng());

					SheetRender sr = new SheetRender(worksheet, options);
					sr.toImage(0, request.getImageOutputFilePath());
				}
			}

Hi,

Thanks for the sample code and details.

Could you provide us template Excel file containing the PivotTable, we will check it soon.

Thank you.

sample.zip (13.1 KB)

@jsaunders2011,

Thanks for the template file.

Well, you got to refresh and calculate PivotTable data before rendering to image file format. Please add the following lines to your code segment, it would work fine as I tested:
e.g
Sample code:

>             PivotTableCollection pivotTables = worksheet.getPivotTables();
> 			Iterator<PivotTable> it = pivotTables.iterator();

> 			while (it.hasNext()) {
> 				PivotTable pivotTable = it.next();
//Add the following lines to refresh data and calculate data.
                                    pivotTable.refreshData();
                                    pivotTable.calculateData(); 

> 				System.out.println("doSnapshotByName() :: " + pivotTable.getName());
> 				CellArea pivotArea = pivotTable.getTableRange1();
> 				System.out.println("doSnapshotByName() :: pivot table area :: " + pivotArea);
> 				if (request.getElementName().equalsIgnoreCase(pivotTable.getName())) {
> 					System.out.println("doSnapshotByName() :: found pivot table :: " + pivotTable.getName());
> 					worksheet.getPageSetup().setPrintArea(buildPrintArea(pivotArea));
> 					worksheet.getPageSetup().setLeftMargin(0.0);
> 					worksheet.getPageSetup().setRightMargin(0.0);
> 					worksheet.getPageSetup().setTopMargin(0.0);
> 					worksheet.getPageSetup().setBottomMargin(0.0);
> 					
> 					if (request.getImageOutputFilePath().contains(".emf"))
> 						options.setImageFormat(ImageFormat.getEmf());
> 					else
> 						options.setImageFormat(ImageFormat.getPng());

> 					SheetRender sr = new SheetRender(worksheet, options);
> 					sr.toImage(0, request.getImageOutputFilePath());
> 				}
> 			}

Hope, this helps a bit.

Thank you.