Optimize SVG images in Aspose.Cells for Java API

Hello,

For performance and bandwidth purposes, we need to optimize the sizes of SVG images. It would be great if there were additional ImageOrPrintOptions that allow us to optimize SVG images.

For example, currently the SVG XML generated by Cells includes extraneous whitespace which increases the SVG image size. This is demonstrated by using the attached Workbook SvgShapes.xlsx (12.7 KB) and the following code using Cells for Java version 19.11:

final String xlFile = [WB_PATH] + "SvgShapes.xlsx";
final String shapeName = "Chart 1";

Workbook wb = new Workbook(xlFile);
Shape shape = wb.getWorksheets().get(0).getShapes().get(shapeName);

// Convert the Shape to SVG
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setSaveFormat(SaveFormat.SVG);
options.setImageType(ImageType.SVG);
options.setDesiredSize(shape.getWidth(), shape.getHeight());

ByteArrayOutputStream baos = new ByteArrayOutputStream();
shape.toImage(baos, options);
byte[] imgBytes = baos.toByteArray();

// Save the ORIGINAL SVG file
Path svgFile = Paths.get(xlFile.replace(".xlsx", "_" + shapeName + "_orig.svg"));
Files.deleteIfExists(svgFile);
Files.write(svgFile, imgBytes);
System.out.format("ORIGINAL SVG image size: %,d bytes.%n", imgBytes.length);
System.out.format("Saved original SVG as: %s%n", svgFile.toString());

// Remove extraneous whitespace
String svgStr = new String(imgBytes, StandardCharsets.UTF_8);
svgStr = svgStr.substring(svgStr.indexOf("<svg "));
svgStr = svgStr.trim().replaceAll("\\n\\s+", "");
imgBytes = svgStr.getBytes(StandardCharsets.UTF_8);

// Re-save trimmed SVG
svgFile = Paths.get(xlFile.replace(".xlsx", "_" + shapeName + "_trimmed.svg"));
Files.deleteIfExists(svgFile);
Files.write(svgFile, imgBytes);
System.out.format("TRIMMED SVG image size: %,d bytes.%n", imgBytes.length);
System.out.format("Saved trimmed SVG as: %s%n", svgFile.toString());

Running the code above, shows the file sizes before and after removing the whitespace.

However, please also consider additional optimizations (beyond whitespace removal) to make the SVG even more compact. For example, a Google search for “reduce SVG file size” yields several relevant results with SVG optimization solutions.

Thank you.

@oraspose,

Thanks for the template file and sample code segment.

After an initial test, I noticed the behavior you described via your code segment with template file. I found the SVG XML generated by Aspose.Cells includes extraneous whitespace which increases the SVG image size. I have logged a ticket with an id “CELLSJAVA-43064” for your requirements. We will check if we could add additional options for ImageOrPrintOptions or for other APIs to optimize SVG images further.

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

Hi @Amjad_Sahi. Just following up on this issue and wondering why this issue was marked as suspended. We believe it would be a beneficial enhancement for users whom need to optimize the bytes sent through the wire.
Thanks.

@oraspose,

Yes, the ticket is temporary suspended. We will give more details and updates on it soon.

@oraspose,

After looking into the generated SVG file, we found that the definition of style (e.g. style for Path, Text, etc.) has a lot of room for improvement, e.g., trying to move styles to CSS. However, this needs a lot of work to be done. The work is suspended because of other important tasks (on hand) with higher priority.

Anyway, we will update you when we start working on it or we have some progress on it.

Thank you for the update @Amjad_Sahi.
This will be a welcomed enhancement when it becomes available.

@oraspose,

You are welcome.

@oraspose

This is to inform you that your issue has been resolved now. The fix will be included in our upcoming release (Aspose.Cells v23.6) that we plan to release in the first half of June 2023. You will be notified when the next version is released.

Here is the result(about 14KB) for your preview with the following code(Enable opitimization by options.setOptimized(true);) :

Workbook wb = new Workbook(xlFile);
Shape shape = wb.getWorksheets().get(0).getShapes().get(shapeName);

// Convert the Shape to SVG
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setImageType(ImageType.SVG);
options.setDesiredSize(shape.getWidth(), shape.getHeight(), false);
// Enable optimization
options.setOptimized(true);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
shape.toImage(baos, options); 

output_prefix.zip (2.3 KB)

The issues you have found earlier (filed as CELLSJAVA-43064) have been fixed in Aspose.Cells for Java 23.6.