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

In today's world of document automation and business reporting, the ability to programmatically manage document content is the key to improving efficiency and creating customized solutions. One of these tools is the SautinSoft Excel .NET library, which allows you to work with Excel files at the program code level, automatically add new elements and create unique content.

In this article, we'll look at how to insert a custom shape into an existing or new document using this library.

You may need to insert graphic elements, such as shapes, directly into an Excel document in the following cases:

  • Automation of reports and presentations: adding logos, arrows, highlights.
  • Creation of templates with built-in graphic elements: a form for data entry, graphical hints.
  • Visualization of data and business scripts: custom arrows, blocks or diagrams.
  • Educational materials: insert illustrations or diagrams for teaching.
  • Marketing materials: inserting branded elements into the documents being prepared.

Using automated methods allows not only to save time, but also to ensure the same quality of registration for consecutive documents.

Step-by-Step:

  1. Add SautinSoft.Excel from Nuget.
  2. Load a xlsx document and add Worksheets.
  3. Insert a shape.
  4. Save a 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 custom shape inside.
        /// </summary>
		/// <remarks>
        /// Details: https://sautinsoft.com/products/excel/help/net/developer-guide/drawings/insert-custom-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.Red);
            property.Outline.Fill.SetSolid(SKColors.Black);

            var custom = property.Geometry.SetCustom();
            var path = custom.AddPath(new SautinSoft.Excel.Drawing.Size(200, 200));
            path.MoveTo(new SautinSoft.Excel.Drawing.Point(100, 50));
            path.AddCubicBezier(new SautinSoft.Excel.Drawing.Point(50, 0),
                new SautinSoft.Excel.Drawing.Point(0, 50),
                new SautinSoft.Excel.Drawing.Point(100, 200));
            path.AddCubicBezier(new SautinSoft.Excel.Drawing.Point(200, 50),
                new SautinSoft.Excel.Drawing.Point(150, 0),
                new SautinSoft.Excel.Drawing.Point(100, 50));
            path.ClosePath();

            worksheet.Drawings.Add(shape);
            shape.BoundingRectangle = new SautinSoft.Excel.Drawing.Rectangle(0, 0, 200, 200);

            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.Red)
			[property].Outline.Fill.SetSolid(SKColors.Black)

			Dim custom = [property].Geometry.SetCustom()
			Dim path = custom.AddPath(New SautinSoft.Excel.Drawing.Size(200, 200))
			path.MoveTo(New SautinSoft.Excel.Drawing.Point(100, 50))
			path.AddCubicBezier(New SautinSoft.Excel.Drawing.Point(50, 0), New SautinSoft.Excel.Drawing.Point(0, 50), New SautinSoft.Excel.Drawing.Point(100, 200))
			path.AddCubicBezier(New SautinSoft.Excel.Drawing.Point(200, 50), New SautinSoft.Excel.Drawing.Point(150, 0), New SautinSoft.Excel.Drawing.Point(100, 50))
			path.ClosePath()

			worksheet.Drawings.Add(shape)
			shape.BoundingRectangle = New SautinSoft.Excel.Drawing.Rectangle(0, 0, 200, 200)

			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.