How to add fallback image for SVG in Excel document using C#?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.
This article explains how to add fallback image for SVG in Excel document using C#.
Steps for adding fallback image for SVG in Excel document programmatically:
Step 1: Create a new C# console application project.
Step 2: Install the Syncfusion.XlsIO.Net.Core, Skiasharp and Svg.Skia NuGet package as reference to your .NET Core applications from NuGet.org.
Step 3: Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
using SkiaSharp;
using Svg.Skia;
Step 4: Include the following code snippet to add a SVG image in the Excel document.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Loads a svg image stream
FileStream svgStream = new FileStream(@"../../../Data/Image.svg", FileMode.Open);
//Convert svg stream to png stream
Stream pngStream = ConvertSvgStreamToPngStream(svgStream);
//Add pictures in the worksheet
worksheet.Pictures.AddPicture(1, 1, svgStream, pngStream);
//Saving the workbook as stream
FileStream stream = new FileStream("Svg.xlsx", FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(stream);
stream.Dispose();
}
Step 5: Use the provided helper method to convert the SVG stream to a PNG stream and add it as a fallback image for the SVG image.
C#
public static Stream ConvertSvgStreamToPngStream(Stream svgStream)
{
// Create a MemoryStream to store the converted PNG
MemoryStream pngStream = new MemoryStream();
// Load SVG image using Skiasharp
using (var svg = new SKSvg())
{
svg.Load(svgStream);
// Get the size of SVG image
var bitmap = new SKBitmap((int)svg.Picture.CullRect.Width, (int)svg.Picture.CullRect.Height);
var canvas = new SKCanvas(bitmap);
// Render SVG to the bitmap
canvas.DrawPicture(svg.Picture);
// Encode the bitmap as PNG and write to the MemoryStream
var image = SKImage.FromBitmap(bitmap);
image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(pngStream);
}
return pngStream;
}
A complete working sample of how to add fallback image for SVG in Excel document using C# can be downloaded from here.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheets or workbooks, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to a worksheet using Template Markers, and most importantly PDF and Image conversions with code examples.
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion license key in your application to use the components without a trial message.