Save a single sheet as PDF with Charts

I’m attempting to take a single sheet from a workbook and convert it to pdf. The sheet has a chart. The data for the chart is on a different sheet that I do not want included in the pdf. My approach is to copy the sheet that I want to a new workbook and then save the workbook as a PDF. However, the data on the chart is not included in the pdf with this approach. I confirmed that the chart was still rendered when using v. 8.3.0.4. I am using version 17.3.0.


I’m attaching the sample excel sheet that I can use to reproduce the problem.

Here is my code:

<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Path path = Paths.get(“Model.xlsx”);
Workbook sourceWorkbook = new Workbook(Files.newInputStream(path));
Worksheet sourceSheet = sourceWorkbook.getWorksheets().get(“Valuation”);

Workbook workbookToPDF = new Workbook();
workbookToPDF.getWorksheets().get(0).copy(sourceSheet);

PdfSaveOptions saveOptions = new PdfSaveOptions(SaveFormat.PDF);
String pdfPath = “Test.pdf”;
workbookToPDF.save(pdfPath, saveOptions);

I consider this a defect since it worked in a previous versioon; I’m hoping for a work around.

Regards

Hi,


Thanks for the template file, sample code with details

Well, as you are rendering only one worksheet whose chart’s data source is some different sheet, so chart’s data might not be rendered in the output PDF. I think you may easily cope with situation by hiding your unwanted worksheets (except for your desired chart sheet) and then render your source workbook to PDF file format. See the sample code for your reference, I have tested it and it works fine:
e.g
Sample code:

Workbook sourceWorkbook = new Workbook(“Model.xlsx”);

for (Object sheetObject : sourceWorkbook.getWorksheets()) {
Worksheet sheet = (Worksheet) sheetObject;
System.out.println(sheet.getName());
if (!“Valuation”.equals(sheet.getName()))
{
sheet.setVisibilityType(VisibilityType.HIDDEN);
}
}

PdfSaveOptions saveOptions = new PdfSaveOptions(SaveFormat.PDF);
String pdfPath = “out1.pdf”;
sourceWorkbook.save(pdfPath, saveOptions);

Hope, this helps a bit.

Thank you.

This solved my problem. Thank you very much.


regards,
Dave

Hi,


Good to know that your issue is sorted out by the suggested code segment. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

Thank you.