Not able to add SVG Image in aspose cells java

Hi,

I am trying add, SVG image in a new excel workseet. Below is the code for the same.

String dataDir = “C:\Aspose Cells\”;
Workbook wb = new Workbook();

Worksheet ws = wb.getWorksheets().get(0);

Chart chart = ws.getCharts().get(0);

String svgString = readSvgImageFile(“C:\Aspose\ChartsSVG\image.svg”);

OutputStream svgOS = addSVGImageInStringFashionOutputStream(svgString);

ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setSaveFormat(SaveFormat.SVG);
**chart.toImage(svgOS, options); **

// Save the file
wb.save(dataDir + “Image_SVG_Sample.xlsx”)

Exception: at line Chart chart = ws.getCharts().get(0);

Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
_ at java.util.ArrayList.RangeCheck(Unknown Source)_
_ at java.util.ArrayList.get(Unknown Source)_
_ at com.aspose.cells.ChartCollection.get(Unknown Source)_

Please let know, what needs to be done with respect to Chart object.

Thanks,
Balakumar

@BalakumarSeethapathy,

The error is due to you own code. Since you are creating a new Workbook and you have not added any chart in the first worksheet, so how could you get the chart in the sheet using the line:
i.e.,
Chart chart = ws.getCharts().get(0);

So, the exception is rightly thrown on the above line.

Let us know if you still have any confusion or issue.

Hi,

Yes, you are right. I am able to add other format images (like JPEG) with new workbook and it works like a charm. The same logic/code doesn’t work for adding SVG images with new workbook.

The code example given in the link also shows chart to image and not for adding SVG images to new workbook. Converting Chart to Image in SVG Format|Documentation

Could you let me know if there’s way to add SVG images with new Workbook.

Thanks for your help in advance!

Regards,
Balakumar

@BalakumarSeethapathy,

I am still not able to reproduce your issue. I have tested the following sample code with the attached template file (which contains a chart in the first worksheet), it works fine and the output SVG image is fine (attached).
e.g
Sample code:

Workbook workbook = new Workbook("chart.xlsx");

// Access the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

// Access the first chart inside the worksheet
Chart chart = worksheet.getCharts().get(0);

// Save the chart into image in SVG format
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setSaveFormat(SaveFormat.SVG);
chart.toImage("out1.svg", options);

I am using our latest version/fix: Aspose.Cells for Java v17.9 (latest version), please try it if you are not already using it.
If you still find the issue, kindly do share your sample code (runnable) with template file (if any), we will check it soon.
files1.zip (11.6 KB)

Hi,

The above example that you gave works for me as well.

Let me try to rephrase my requirement for better understanding.

I have a SVG image and the whole SVG xml is available as a String.
I need to insert the same in a new work book which may not have any pre-loaded chart template in it. Is it possible is Aspose.Cells?

The API example and the sample code given by you needs a chart template to insert the SVG image., but I need to insert SVG image without any pre existing chart template in the work sheet.

Sorry to bother you on this one again! Thanks for your help!!

Regards,
Balakumar

@BalakumarSeethapathy,

Could you try to put your source xml in some file (you may open a new file into notepad) manually and then save it to e.g “image.svg”. Now open the file into some browser to check if the SVG file is fine or not? This way, you may confirm if your SVG xml string is ok or not. Moreover, you should insert the SVG file (e.g “image.svg” (using its file path) to add to the worksheet via Aspose.Cells.

If you still find the issue, kindly do provide sample JAVA program (runnable), resource files (SVG, MS Excel file(s), etc.) to show the issue on our end, we will check it soon.

Hi,

I have attached the sample code I was using to insert a svg image and jpg image to excel work sheet. Please take a look at the code.

You can uncomment line no: 46 and comment line no: 47 to see the it works for “pie chart1.jpg” image, but for inserting “dynamicsSVGimage.svg” it throws error as unknown image format.

I inspected, Picture object to set the image format, but couldn’t find the right API.

Thanks,
Balakumarsample code and Image files.zip (6.6 KB)

@BalakumarSeethapathy,

Thanks for providing us sample code and image files.

I explored the feature/issue in MS Excel manually and found MS Excel 2003 - 2013 does not support to insert SVG image type into worksheet cell(s). In short, MS Excel itself does not support SVG image format in it. Regarding MS Excel 2016, it looks like to be supported. I have logged a ticket with an id “CELLSJAVA-42413” for the issue/requirements. Our product team will evaluate it and look into it if we could support it or feature is feasible to be supported in MS Excel formats.

Once we have an update on it, we will let you know here.

Hi,

Thanks for taking looking into this. I may try to upgrade my MS Office 2016 and try SVG images.

Regards,
Balakumar

@BalakumarSeethapathy,

This is to inform you that we have fixed your issue now. We will soon provide you the fixed version after performing QA and incorporating other enhancements and fixes.

Keep in touch.

The issues you have found earlier (filed as CELLSJAVA-42413) have been fixed in this update. This message was posted using BugNotificationTool from <a href=“https://#{request.env[“HTTPS_HOST”]}”>Downloads module by Amjad_Sahi

The issues you have found earlier (filed as CELLSJAVA-42413) have been fixed in Aspose.Cells for Java 18.4. Please also see the document for your reference: