PivotChart are not updated before export to PDF

I want to create a PDF from a XLSX (input.xlsx). The program writes some values into the Excel sheet, updates the pivot table and saves the excel as output.xlsx and output.pdf.

The file output.xlsx is correct shows the updated values (in Pivot Chart and Pivot Table), but the output.pdf has an incorrect Pivot Chart/Image. It still shows the source Pivot Chart from input.xlsx without the updated values.

        val asposeLicense = File("./Aspose.Total.Java.lic")
        License().setLicense(FileUtils.openInputStream(asposeLicense))

        val workbook = Workbook("./input.xlsx")

        val worksheet = workbook.worksheets[0]
        for (i in 1..31) {
            worksheet.cells[i + 14, 1].putValue(i)
        }
        val pivotTable = worksheet.pivotTables["PivotTable1"]
        pivotTable.refreshData()
        pivotTable.calculateData()
        worksheet.refreshPivotTables()

        workbook.calculateFormula()
        workbook.save("./output.xlsx")

        // save as pdf
        val pdfSaveOptions = PdfSaveOptions()
        val sheetSet = SheetSet(intArrayOf(0))
        pdfSaveOptions.calculateFormula = true
        pdfSaveOptions.sheetSet = sheetSet
        workbook.save("./output.pdf", pdfSaveOptions)

How can I tell Aspose to update the PivotChart before exporting to PDF?

Source:
TestExportToPdf.zip (47,2 KB)
This shows the problem:
problem.jpg (382,0 KB)

Used aspose version:

        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-cells</artifactId>
            <version>24.9</version>
        </dependency>

@adiesner,

Thanks for the template Excel file and output files, screenshots and details.

To ensure the pivot chart displays correctly and remains up-to-date, you need to refresh it programmatically in code. You can achieve this by using the Chart.refreshPivotData() method, which updates the pivot chart based on the latest pivot data or values. Refer to the highlighted line in the following sample code for guidance.


PivotTable pivotTable = worksheet.getPivotTables().get(“PivotTable1”);
pivotTable.refreshData();
pivotTable.calculateData();
worksheet.refreshPivotTables();

workbook.calculateFormula();
worksheet.getCharts().get(0).refreshPivotData();

Let us know if you still find any issue.

1 Like

@adiesner
Please change codes as the following :

//var pivotTable = worksheet.PivotTables["PivotTable1"];
//pivotTable.RefreshData();
//pivotTable.CalculateData();
//worksheet.RefreshPivotTables();

workbook.CalculateFormula();
workbook.Worksheets.RefreshAll();
1 Like

Thanks to both of you, both your answers solve my problem!

@adiesner
Thank you for your feedback. You are welcome. I’m glad you solved the issue by referring to the provided sample code. If you have any questions, please feel free to contact us at any time.