Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 5, 2026 18:25
Show Gist options
  • Select an option

  • Save aspose-com-gists/14bffe6fc52688c48540a7690915381e to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/14bffe6fc52688c48540a7690915381e to your computer and use it in GitHub Desktop.
This Gist contains C# examples Aspose.SVG for .NET – Using SVG Builder

Aspose.SVG for .NET – C# Examples for SVG Builder

This GitHub gist repository contains C# code samples that demonstrate how to use the powerful SVG Builder API in the Aspose.SVG for .NET library. These examples are used in the SVG Builder – Advanced SVG Creation and Modification chapter of the official Aspose.SVG for .NET documentation and allow .NET developers to programmatically create, modify, and customize SVG documents with exceptional control and flexibility.

What’s Inside?

Here you will find practical demonstrations of creating, modifying, and fine-tuning SVG documents with an exceptional level of programmatic control, including:

  • SVG Element Creation – Create SVG structures from scratch, including basic shapes (circles, rectangles, paths), text, and groups through a fluent SVG Builder API.
  • Working with Geometric Shapes – Create and modify basic SVG shapes, including circles, rectangles, and paths.
  • Path Data Manipulation – Work directly with SVG path data for intricate shape creation and modification.
  • Configuring SVG Styles – Define custom fonts (@font-face) and apply CSS rules to text elements within an SVG document using the SVG Builder API.
  • Working with SVG Patterns – Set and apply various SVG patterns (<pattern>) to fill shapes like circles, etc.
  • Applying CSS Rules – Define CSS rules for SVG elements (e.g., circle, text) using the Rule Builder API. Set properties like fill, stroke, stroke-width, font-family, and font-size directly within the C# code to style SVG components.

Quick Start

Each gist serves as a standalone, executable example designed to help you quickly understand and implement SVG functionalities in your .NET applications.

  1. Ensure that you have the Aspose.SVG for .NET library installed.
  2. Locate the example that corresponds to your specific task.
  3. Copy the C# code directly from the example.
  4. Adjust the file paths, input parameters, and output parameters as necessary.
  5. Run the code to experience the powerful SVG manipulation capabilities.

Prerequisites

  • .NET Platforms: .NET Framework 4.6.1+, .NET Core 2.0+, or .NET 5+.
  • Supported OS: Windows, Linux, macOS.
  • Aspose.SVG for .NET installed via NuGet.

About Aspose.SVG for .NET

Aspose.SVG for .NET is a powerful, on-premise library designed for creating, parsing, processing, and converting SVG files. Its SVG Builder API provides a fluent and flexible approach to programmatically build and modify complex SVG documents, striking a balance between precision and efficiency.

Resources

Aspose.SVG for .NET – Using SVG Builder
// Add multiple shapes (circle and polyline) to an existing SVG document using SVG Builder
//Learn more: https://docs.aspose.com/svg/net/element-builders/
string documentPath = Path.Combine(DataDir, "circles.svg");
// Initialize an SVG document
using (SVGDocument document = new SVGDocument(documentPath))
{
// Create a <circle> element with attributes
SVGCircleElement circle = new SVGCircleElementBuilder()
.Cx(350).Cy(70).R(50).Fill(Paint.None).Stroke(Color.FromArgb(80, 132, 132)).StrokeWidth(10)
.Build(document);
// Append the newly created <circle> element to the root <svg> element
document.RootElement.AppendChild(circle);
// Create a <polyline> element with attributes
SVGPolylineElement polyline = new SVGPolylineElementBuilder()
.Points(new double[] { 125, 130, 275, 180, 425, 130 }).Stroke(Color.FromArgb(80, 132, 132)).Fill(Paint.None).StrokeWidth(10)
.Build(document);
// Append the newly created <polyline> element to the root <svg> element
document.RootElement.AppendChild(polyline);
// Save the document
document.Save(Path.Combine(OutputDir, "circles-edited.svg"));
}
// Initialize an SVG document
using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "tulips.svg")))
{
SVGSVGElement svg = new SVGSVGElementBuilder()
.Width(100, LengthType.Percentage)
.Height(100, LengthType.Percentage)
.Build(document.FirstChild as SVGSVGElement);
// Create a new <g> element using SVGGElementBuilder and add <style> and <rect> elements to it
SVGElement g = new SVGGElementBuilder()
.Id("backgound")
.AddStyle(style => style
.Type("text/css")
.AddRule(".background", r => r.Fill(Color.Gray))
)
.AddRect(rect => rect
.X(-120).Y(0).Width(100, LengthType.Percentage).Height(100, LengthType.Percentage)
.Class("background")
).BuildElement(document);
svg.InsertBefore(g, svg.FirstElementChild);
// Save the document
document.Save(Path.Combine(OutputDir, "add-background-color-with-builder.svg"));
}
// Configure an SVG document's style with external font-face rules in C# using Rule Builder
//Learn more: https://docs.aspose.com/svg/net/rule-builder/
// Initialize an SVG document
using (SVGDocument document = new SVGDocument())
{
// Create an <svg> element and use Builder API to add other rules and elements to it
SVGSVGElement svg = new SVGSVGElementBuilder()
.AddDefs(def => def
.AddStyle(st => st
.Type("text/css")
.AddRule("@font-face", r => r
.FontFamily("VeinlineRegular")
.Attribute("src", "url(https://assets.ithillel.ua/wiki/VeinlineRegular.ttf)")
)
.AddRule("text", t => t
.FontFamily("VeinlineRegular")
.FontSize(30, LengthType.Pt)
)
)
)
.AddText(t => t
.X(20).Y(60).Fill(Color.DarkRed).FontWeight(FontWeight.Bold)
.AddContent("Configure SVG Style")
)
.AddText(t => t
.X(20).Y(150)
.AddContent("Set your SVG style with Rule Builder!")
)
.Build(document.FirstChild as SVGSVGElement);
// Save the SVG document
document.Save(Path.Combine(OutputDir, "font-face.svg"));
}
// Create all basic SVG shapes programmatically in C# using SVG Builder
//Learn more: https://docs.aspose.com/svg/net/element-builders/
string svgContent = "<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>";
using (SVGDocument document = new SVGDocument(svgContent, "."))
{
SVGSVGElement svg = new SVGSVGElementBuilder()
.Width(100, LengthType.Percentage).Height(100, LengthType.Percentage)
.ViewBox(0, 0, 800, 800)
.AddG(g => g.FontSize(20).TextAnchor(TextAnchor.End)
.AddText("<rect>", y: 30)
.AddText("<circle>", y: 70)
.AddText("<ellipse>", y: 110)
.AddText("<line>", y: 150)
.AddText("<polyline>", x: 300, y: 30)
.AddText("<polygon>", x: 300, y: 70)
.AddText("<path>", x: 300, y: 110)
)
.AddG(gg => gg.Fill(Color.Teal).StrokeWidth(3)
.AddRect(r => r.X(35).Y(5).Width(30).Height(30))
.AddCircle(c => c.Cx(50).Cy(60).R(17))
.AddEllipse(e => e.Cx(50).Cy(100).Rx(25).Ry(12))
.AddLine(l => l.X1(30).X2(70).Y1(140).Y2(140).Stroke(Color.Teal))
)
.AddG(ggg => ggg.Fill(Paint.None).StrokeWidth(3).Stroke(Color.Teal).Transform(t => t.Translate(300, -160))
.AddPolygon(points: new double[] { 30, 215, 90, 215, 120, 230, 70, 240, 30, 215 }, fill: Color.Teal)
.AddPolyline(points: new double[] { 30, 200, 65, 170, 90, 190, 120, 180 })
.AddPath(d: path => path.M(30, 275).Q(55, 250, 70, 250).T(80, 275).T(120, 260))
)
.Build(document.FirstChild as SVGSVGElement);
document.Save(Path.Combine(OutputDir, "svg-elements.svg"));
}
// Create a complex SVG using shapes, gradients, and reusable elements in C#
// Learn more: https://docs.aspose.com/svg/net/svg-builder-api/
// Initialize an SVG document
using (SVGDocument document = new SVGDocument())
{
// Create an <svg> element with specified width, height and viewBox, and add into it other required elements
SVGSVGElement svg = new SVGSVGElementBuilder()
.Width(100).Height(100)
.ViewBox(-21, -21, 42, 42)
.AddDefs(def => def
.AddRadialGradient(id: "b", cx: .2, cy: .2, r: .5, fx: .2, fy: .2, extend: ev => ev
.AddStop(offset: 0, stopColor: Color.FromArgb(0xff, 0xff, 0xFF), stopOpacity: .7)
.AddStop(offset: 1, stopColor: Color.FromArgb(0xff, 0xff, 0xFF), stopOpacity: 0)
)
.AddRadialGradient(id: "a", cx: .5, cy: .5, r: .5, extend: ev => ev
.AddStop(offset: 0, stopColor: Color.FromArgb(0xff, 0xff, 0x00))
.AddStop(offset: .75, stopColor: Color.FromArgb(0xff, 0xff, 0x00))
.AddStop(offset: .95, stopColor: Color.FromArgb(0xee, 0xee, 0x00))
.AddStop(offset: 1, stopColor: Color.FromArgb(0xe8, 0xe8, 0x00))
)
)
.AddCircle(r: 20, fill: "url(#a)", stroke: Color.FromArgb(0, 0, 0), extend: c => c.StrokeWidth(.15))
.AddCircle(r: 20, fill: "b")
.AddG(g => g.Id("c")
.AddEllipse(cx: -6, cy: -7, rx: 2.5, ry: 4)
.AddPath(fill: Paint.None, stroke: Color.FromArgb(0, 0, 0), d: "M10.6 2.7a4 4 0 0 0 4 3", extend: e => e.StrokeWidth(.5).StrokeLineCap(StrokeLineCap.Round))
)
.AddUse(href: "#c", extend: e => e.Transform(t => t.Scale(-1, 1)))
.AddPath(d: "M-12 5a13.5 13.5 0 0 0 24 0 13 13 0 0 1-24 0", fill: Paint.None, stroke: Color.FromArgb(0, 0, 0), extend: e => e.StrokeWidth(.75))
.Build(document.FirstChild as SVGSVGElement);
// Save the SVG document
document.Save(Path.Combine(OutputDir, "face.svg"));
}
// Create SVG with styled circle and text elements using C# Rule Builder API and fluent syntax
//Learn more: https://docs.aspose.com/svg/net/rule-builder/
// Initialize an SVG document
using (SVGDocument document = new SVGDocument())
{
// Create an <svg> element and use Builder API to add other rules and elements to it
SVGSVGElement svg = new SVGSVGElementBuilder()
.AddStyle(st => st
.AddRule("circle", r => r
.Fill(paint: Paint.None)
.Stroke(color: Color.DarkRed)
.StrokeWidth(60)
)
.AddRule("text", t => t
.Fill(color: Color.Red)
.Stroke(color: Color.Teal)
.StrokeWidth(1)
.FontFamily("Arial")
.FontSize(30, LengthType.Pt)
)
)
.AddG(g => g
.AddCircle(circle => circle.Cx(100).Cy(130).R(60).StrokeDashArray(2, 14))
.AddText("Rule Builder", x: 230, y: 140)
)
.Build(document.FirstChild as SVGSVGElement);
// Save the SVG document
document.Save(Path.Combine(OutputDir, "rule-builder.svg"));
}
// Create SVG from scratch in C# using Builder API
// Learn more: https://docs.aspose.com/svg/net/element-builders/
// Create an <svg> element with specified width, height and viewBox, and add into it other required elements
SVGSVGElement svg = new SVGSVGElementBuilder()
.Width(700).Height(300)
.ViewBox(0, 0, 700, 300)
.AddG(g => g
.AddCircle(circle => circle.Cx(130).Cy(130).R(60).Fill(Paint.None).Stroke(Color.FromArgb(200, 0, 0)).StrokeWidth(70).StrokeDashArray(2, 14))
.AddText("I can create SVG from scratch!", x: 270, y: 140, fontSize: 30, fill: Color.Teal)
)
.Build(document.FirstChild as SVGSVGElement);
// Save the document
document.Save(Path.Combine(OutputDir, "svg-from-scratch.svg"));
}
// Create an SVG path element with line commands programmatically in C# using SVG Path Builder
//Learn more: https://docs.aspose.com/svg/net/path-builder/
// Initialize an SVG document
using (SVGDocument document = new SVGDocument())
{
// Create an <svg> element with specified width, height and viewBox, and add a <path> element to it
SVGSVGElement svg = new SVGSVGElementBuilder()
.Width(200).Height(200)
.ViewBox(0, 0, 150, 150)
.AddPath(p => p
// 'D' method defines the 'd' attribute, which contains the path data
.D(d => d
// 'M' sets the starting point of the path (Move to x=50, y=50)
.M(50, 50)
// 'L' draws a line from the current point to the new point (Line to x=100, y=50)
.L(100, 50)
// Another 'L' to draw a line to a new point (Line to x=100, y=100)
.L(100, 100)
// 'Z' closes the path by drawing a line to the starting point
.Z())
// Sets the fill color of the path to teal
.Fill(Color.Teal))
.Build(document.FirstChild as SVGSVGElement);
// Save the SVG document
document.Save(Path.Combine(OutputDir, "path.svg"));
}
// Create SVG pattern with polygons and apply as fill to rectangle using C# Builder API
//Learn more: https://docs.aspose.com/svg/net/paint-builder/
using (SVGDocument document = new SVGDocument())
{
SVGSVGElement svg = new SVGSVGElementBuilder()
.Width(100, LengthType.Percentage).Height(100, LengthType.Percentage)
.ViewBox(0, 0, 400, 400)
.AddG(g => g
.AddPattern(p => p.Id("stars")
.ViewBox(0, 0, 20, 20)
.Width(5, LengthType.Percentage)
.Height(5, LengthType.Percentage)
.PatternUnits(CoordinateUnits.UserSpaceOnUse)
.AddPolygon(points: new double[] { 5, 0, 7, 3, 10, 5, 7, 7, 5, 10, 3, 7, 0, 5, 3, 3 }, fill: Color.Teal)
.AddPolygon(points: new double[] { 15, 0, 17, 3, 20, 5, 17, 7, 15, 10, 13, 7, 10, 5, 13, 3 }, fill: Color.DarkRed)
.AddPolygon(points: new double[] { 15, 10, 17, 13, 20, 15, 17, 17, 15, 20, 13, 17, 10, 15, 13, 13 }, fill: Color.DarkBlue)
)
.AddRect(r => r.Rect(20, 40, 440, 80).Fill(pt => pt.PaintServerId("stars")))
)
.Build(document.FirstChild as SVGSVGElement);
document.Save(Path.Combine(OutputDir, "pattern-stars.svg"));
}
// Create SVG patterns with reusable elements and apply them to circles using C# Builder API
//Learn more: https://docs.aspose.com/svg/net/paint-builder/
using (SVGDocument document = new SVGDocument())
{
SVGSVGElement svg = new SVGSVGElementBuilder()
.Width(100, LengthType.Percentage).Height(100, LengthType.Percentage)
.ViewBox(0, 0, 600, 600)
.AddG(g => g
.FontFamily("Arial")
.FontSize(10)
.AddPattern(p => p.Id("Pat3a")
.Rect(0, 0, 20, 20)
.PatternUnits(CoordinateUnits.UserSpaceOnUse)
.AddRect(r => r.Rect(0, 0, 10, 10).Fill(Color.LightSlateGray))
.AddRect(r => r.Rect(10, 0, 10, 10).Fill(Color.Teal))
.AddRect(r => r.Rect(0, 10, 10, 10).Fill(Color.DarkRed))
.AddRect(r => r.Rect(10, 10, 10, 10).Fill(Color.Gold))
)
.AddPattern(p => p.Id("Pat3b")
.Href("#Pat3a")
.Width(23).Height(23)
)
.AddPattern(p => p.Id("Pat3c")
.Href("#Pat3a")
.Width(15).Height(15)
)
.AddCircle(circle => circle.Cx(90).Cy(130).R(70).Fill(pt => pt.PaintServerId("Pat3a")))
.AddText(t => t.X(55).Y(50)
.AddContent("Pattern #Pat3a")
)
.AddCircle(circle => circle.Cx(240).Cy(130).R(70).Fill(pt => pt.PaintServerId("Pat3b")))
.AddText(t => t.X(205).Y(50)
.AddContent("Pattern #Pat3b")
)
.AddCircle(circle => circle.Cx(390).Cy(130).R(70).Fill(pt => pt.PaintServerId("Pat3c")))
.AddText(t => t.X(355).Y(50)
.AddContent("Pattern #Pat3c")
)
)
.Build(document.FirstChild as SVGSVGElement);
document.Save(Path.Combine(OutputDir, "patterns.svg"));
}
// Create SVG text path in C# using SVG Builder
//Learn more: https://docs.aspose.com/svg/net/path-builder/
// Initialize an SVG document
using (SVGDocument document = new SVGDocument())
{
// Create an <svg> element with specified width, height, and viewBox, and add into it <path> elements
SVGSVGElement svg = new SVGSVGElementBuilder()
.Width(1200).Height(300)
.ViewBox(0, 0, 1200, 300)
.AddPath(p => p.Id("Path1")
.Fill(f => f.None()).Stroke(Color.IndianRed).Transform(t => t.Translate(-100, 40))
.D(d => d.AddPathSegment("M 199 89 C 206 66 235 25 270 30 286 38 298 59 310 73 321 87 338 99 356 103 387 111 396 90 410 85"))
)
.AddPath(p => p.Id("Path2")
.Fill(f => f.None()).Stroke(Paint.None).Transform(t => t.Translate(400, -100))
.D(d => d.M(80, 210).Q(95, 110, 200, 200).T(400, 200)))
.AddText(t => t.FontSize(30).Fill(Color.Teal)
.AddTextPath(tp => tp
.Href("#Path1")
.AddContent("SVG textPath element")
)
.AddTextPath(tp => tp
.Href("#Path2")
.AddContent("SVG can place text along a path")
)
)
.Build(document.FirstChild as SVGSVGElement);
// Save the SVG document
document.Save(Path.Combine(OutputDir, "text-path.svg"));
}
// Modify an existing SVG element in C# using SVG Builder
//Learn more: https://docs.aspose.com/svg/net/element-builders/
string documentPath = Path.Combine(DataDir, "circles.svg");
// Initialize an SVG document
using (SVGDocument document = new SVGDocument(documentPath))
{
// Assume an existing SVG element is part of the document
SVGCircleElement circle = document.GetElementsByTagName("circle").First() as SVGCircleElement;
// Modify the first <circle> element using SVGCircleElementBuilder
new SVGCircleElementBuilder()
.Stroke(Color.DarkRed).Fill(Color.LightGray)
// The first <circle> element is now updated with new configurations
.Build(circle);
// Save the document
document.Save(Path.Combine(OutputDir, "circles-modified.svg"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment