In the modern world of document management automation, it is important not only to create tables and reports, but also to decorate them with graphic elements, to make them more visual and professional. One of these elements are shapes — diagrams, arrows, frames, and other graphic objects that help emphasize important points or structure information.
In this article, we will discuss in detail how to use C# and .Net to insert a shape into a document using the SautinSoft Excel .NET library.
This functionality is useful for automating the creation of reports, presentations, technical documents, or tutorials, which often require visual elements to better understand information. This is especially important if you need to generate documents en masse, for example, reporting forms for hundreds of performers or automated systems.
As for the practice, shape insertion is becoming popular in automated systems, where every action should be automated as much as possible in order to avoid mistakes and speed up the document preparation process.
When is it useful to insert shapes into documents:
Step-by-step guide:
Complete code
using SautinSoft.Excel;
using System.IO;
using SkiaSharp;
namespace Example
{
class Program
{
static void Main(string[] args)
{
// Get your free key here:
// https://sautinsoft.com/start-for-free/
InsertShape();
}
/// <summary>
/// Create xlsx file with a shape inside.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/excel/help/net/developer-guide/drawings/insert-shape-in-excel-csharp-vb.php
/// </remarks>
static void InsertShape()
{
string outFile = @"..\..\..\Result.xlsx";
ExcelDocument excelDocument = new ExcelDocument();
excelDocument.Worksheets.Add("Page 1");
var worksheet = excelDocument.Worksheets["Page 1"];
// Insert a shape
ShapeProperty property = new ShapeProperty();
ExcelShape shape = new ExcelShape(property);
property.Fill.SetSolid(SKColors.Blue);
property.Geometry.SetPreset(SautinSoft.Excel.Drawing.Figure.Ellipse);
property.Outline.Fill.SetSolid(SKColors.Black);
worksheet.Drawings.Add(shape);
shape.BoundingRectangle = new SautinSoft.Excel.Drawing.Rectangle(0, 0, 200, 300);
excelDocument.Save(outFile);
// Important for Linux: Install MS Fonts
// sudo apt install ttf-mscorefonts-installer -y
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
}
}
}
Option Infer On
Imports SautinSoft.Excel
Imports System.IO
Imports SkiaSharp
Namespace Example
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Get your free key here:
' https://sautinsoft.com/start-for-free/
InsertShape()
End Sub
''' <summary>
''' Create xlsx file with a shape inside.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/excel/help/net/developer-guide/insert-images-in-excel-csharp-vb.php
''' </remarks>
Private Shared Sub InsertShape()
Dim outFile As String = "..\..\..\Result.xlsx"
Dim excelDocument As New ExcelDocument()
excelDocument.Worksheets.Add("Page 1")
Dim worksheet = excelDocument.Worksheets("Page 1")
' Insert a shape
Dim [property] As New ShapeProperty()
Dim shape As New ExcelShape([property])
[property].Fill.SetSolid(SKColors.Blue)
[property].Geometry.SetPreset(SautinSoft.Excel.Drawing.Figure.Ellipse)
[property].Outline.Fill.SetSolid(SKColors.Black)
worksheet.Drawings.Add(shape)
shape.BoundingRectangle = New SautinSoft.Excel.Drawing.Rectangle(0, 0, 200, 300)
excelDocument.Save(outFile)
' Important for Linux: Install MS Fonts
' sudo apt install ttf-mscorefonts-installer -y
' Open the result for demonstration purposes.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
End Sub
End Class
End Namespace
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: