Excel files are generally used to work with numeric data. Moreover, charts are rendered to represent data in pictorial form. You can convert a chart to an image or PDF in your Java applications. Aspose.Cells for Java API offer a lot of features to create, edit, or manipulate XLSX, XLS, XLSM, XLSB, and other supported file formats without needing to install Microsoft Excel. In this article, we will be exploring the support for chart rendering to image and PDF file format. Let us outline the features we will be covering here:
- Chart to Image and PDF Conversion API
- Convert a Chart to Image using Java
- Convert all Charts in Worksheets to Images using Java
- Chart to PDF Conversion using Java
Let us learn the details to better understand chart rendering support in the API.
Chart to Image and PDF Conversion API
You can convert or render a chart to different image formats including JPG, PNG, TIFF, or BMP with Aspose.Cells for Java in a few method calls. Likewise, you can render a chart to PDF format as per your requirements. First of all, you need to configure Aspose.Cells for Java API in your Java environment. You can easily install it by downloading the JAR file from Downloads section, or by using the following repository and dependency configurations.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>20.9</version>
</dependency>
Convert a Chart to Image using Java
After configuring the API successfully, you can export or convert a chart to any image format including JPG, PNG, TIFF, BMP, SVG, WMP etc. In this example, let us consider to save chart as a PNG image. You need to follow the following steps for chart to image conversion.
- Load input XLS/XLSX File
- Access the Worksheet and Chart
- Initialize ImageOrPrintOptions to set quality and other properties
- Set image type and convert chart to an Image
The code snippet below shows how to convert a chart to JPG, PNG, TIFF, BMP, SVG or WMP image using Java:
Let us take a look at the screenshot of the chart image generated by this code:
Convert all Charts in Worksheets to Images using Java
We have already learned about rendering single chart to an image. It is a basic use case that we can enhance and cover the scenario where you want to convert all charts in all worksheets to respective images. You need to follow the steps below for converting all charts in a workbook to images:
- Load source XLS/XLSX file
- Iterate through each Worksheet
- Iterate through each chart in each worksheet
- Specify different properties with ImageOrPrintOptions
- Save output image of the Chart
The following code snippet shows how to convert charts to images using Java:
Convert a Chart to PDF using Java
PDF file format is famous because of its compatibility with almost all operating systems. Sometimes you might need to convert a chart from Excel workbook to a PDF file. Although you can convert the previously rendered image to PDF using Aspose APIs, but that would be a two-step approach. Whereas, Aspose.Cells for Java API can directly render the chart as a PDF document. You need to follow the steps below to perform the conversion:
- Load input XLSX file
- Access required Workbook
- Ensure there is at least one Chart
- Render specific chart to PDF
The code snippet below explains how to convert a chart to PDF using Java:
Conclusion
In this article, we have learned how to convert charts in excel files to JPG, PNG, BMP, WMF, or PDF using Java. The API offers a lot of properties and methods to control the rendering of images and PDF documents. We have covered a few of the possible use cases for chart conversion, in case you want to explore more about the rendering features then please feel free to visit API references, product documentation, or Free Support Forum if you need any assistance.