I am attempting to add a lot of SVG Images into a excel sheet using Aspose.Cells. In the process, I came across a property of the Cell called EmbeddedImage
which in theory, should be as simply as placing a byte[] into this property. However, after multiple attempts, i can not get this to work with SVG.
I am looking for a direct solution or workaround to this problem given I have tried multiple Libraries to do what I need done (Such as EPPlus, Office direct reference lib, etc)
Upon setting the EmbeddedImage
, and opening the workbook, it states "we found a problem with some content within the sheet". Clicking No
results in nothing, Clicking Yes
, Excel attempts to "Repair" the workbook, stating the below. All Styles seem to be removed (atleast most of them)
Removed Feature: Worksheet properties from /xl/workbook.xml part (Workbook)
Removed Feature: RichData from /xl/richData/rdrichvalue.xml part (Rich Data Types)
Repaired Records: Cell information from /xl/worksheets/sheet1.xml part
Note: It looks like the reason this is occurring is because the images are actually SVGs
I ended up utilizing a variety of things. However, simply reading the bytes did not work, most likely due to the way this SVG is made. Many of the SVG's i was pulling contained css styles that colored the SVG and those were lost when simply reading bytes. Below was my solution.
// Send the request and get the response
var response = await _httpClient.GetAsync(url);
// Ensure the response is successful
response.EnsureSuccessStatusCode();
// Read the response content as a byte array
var svgDocument = SvgDocument.FromSvg<SvgDocument>(await response.Content.ReadAsStringAsync());
//Set the height and width of what I am reading
svgDocument.Width = 1024;
svgDocument.Height = 1024;
// Set up a Bitmap to render the SVG
using (var bitmap = new Bitmap(1024, 1024))
{
using (var graphics = Graphics.FromImage(bitmap))
{
// Clear the canvas with a white background (optionally use transparent)
graphics.Clear(Color.White);
// Render the SVG content to the bitmap
svgDocument.Draw(graphics);
// Convert the Bitmap to a byte array
using (var memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
return memoryStream.ToArray(); //This is the bytes that was needed
}
}
}