How to insert a shape into a document with C# and .NET

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:

  • Creation of standard reports and forms: for example, highlighting important sections with frames, arrows or colored shapes.
  • Automatic data visualization: adding diagrams and diagrams that help you better understand the structure or dynamics.
  • Educational materials: design of textbooks, presentations, where important elements need to be emphasized or highlighted.
  • Internal instructions and manuals: to make the documentation more understandable and visually rich.
  • Automation of corporate processes: preparation of approval forms, certificates of completed works with graphic elements.

Step-by-step guide:

  1. Add SautinSoft.Excel from Nuget.
  2. Create a page.
  3. Create a shape with some parameters.
  4. Save a resulting document.

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 });
        }
    }
}

Download

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

Download


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:


Captcha

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.