Excel files are generally used to work with numeric data. Moreover, charts are rendered to represent data in pictorial form. Using Java, you can convert a chart to an image or PDF document. 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 in Java. Let us outline the features we will be covering here:

Let us learn the details to better understand chart rendering support in the Java API.

Java API to Convert Chart to Image and PDF

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 Maven 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 in Java:

  1. Load input XLS/XLSX File
  2. Access the Worksheet and Chart
  3. Initialize ImageOrPrintOptions to set quality and other properties
  4. Set image type and convert chart to an Image in Java

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 Chart to image PDF Java

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:

  1. Load source XLS/XLSX file
  2. Iterate through each Worksheet
  3. Iterate through each chart in each worksheet
  4. Specify different properties with ImageOrPrintOptions
  5. 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:

  1. Load input XLSX file
  2. Access required Workbook
  3. Ensure there is at least one Chart
  4. 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.

See Also