Skip to content

Instantly share code, notes, and snippets.

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 – SVG Builder API Examples in C#

This GitHub gist repository contains C# code samples that demonstrate how to use the powerful SVG Builder API. 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.

The snippets show how to build SVG from scratch, create shapes, paths, text, patterns, gradients, filters, clip paths, and CSS rules with fluent C# code.

What's Inside?

Here you will find practical demonstrations of SVG creation and editing with the Builder API, 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.
  • Filters and Clip Paths – Add Gaussian blur, drop shadow filters, and clip paths with SVG Builder.

Quick Start

Each example is a standalone C# test designed to help you quickly understand and implement SVG Builder API features in .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 create or modify SVG files.

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 an API for creating, parsing, editing, rendering, and converting SVG files in C# applications. Its SVG Builder API provides a fluent approach for building and modifying SVG documents with precise control over elements, styles, paths, paints, filters, and reusable graphics.

Resources

Aspose.SVG for .NET – Using SVG Builder
// Create an SVG path segment using SVG Builder in C#
// Learn more: https://docs.aspose.com/svg/net/path-builder/
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(400).Height(400)
.ViewBox(0, 0, 300, 300)
.AddPath(p => p
.Fill(Color.IndianRed)
.D(d => d.AddPathSegment("M10 75 Q150 0 290 75"))
)
.Build(document.FirstChild as SVGSVGElement);
// Save the SVG document
document.Save(Path.Combine(OutputDir, "add-path-segment.svg"));
}
// Combine multiple SVG path segments using SVG Builder in C#
// Learn more: https://docs.aspose.com/svg/net/path-builder/
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(400).Height(400)
.ViewBox(0, 0, 300, 300)
.AddPath(p => p
.Fill(Color.CadetBlue)
.Stroke(Color.Red)
.StrokeWidth(2)
.D(d => d
.AddPathSegment("M40 120")
.AddPathSegment("C70 20 140 20 170 120")
.AddPathSegment("S270 220 300 120")
.AddPathSegment("Q250 180 170 170")
.AddPathSegment("Q90 160 40 120")
)
)
.Build(document.FirstChild as SVGSVGElement);
// Save the SVG document
document.Save(Path.Combine(OutputDir, "add-multiple-path-segments.svg"));
}
// Add circle and polyline elements to an existing SVG using SVG Builder in C#
// 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"));
}
// Add SVG text content using SVG Builder in C#
// Learn more: https://docs.aspose.com/svg/net/svg-builder-api/
using (var document = new SVGDocument())
{
var builder = new SVGSVGElementBuilder();
builder.Attribute("width", "640");
builder.Attribute("height", "260");
builder.AddRect(rect =>
{
rect.X(0);
rect.Y(0);
rect.Width(640);
rect.Height(260);
rect.Fill(Color.FromArgb(245, 247, 251));
});
builder.AddText("Aspose.SVG Builder Text", 60, 110, 34, null, "Arial", null,
Color.Teal, Color.FromArgb(0, 96, 96), "headline", text => text.TextAnchor(TextAnchor.Start));
builder.AddText(text =>
{
text.X(60);
text.Y(170);
text.FontSize(22);
text.FontFamily("Arial");
text.Fill(Color.FromArgb(80, 80, 80));
text.AddContent("Text content added through AddContent.");
});
builder.Build(document.RootElement as SVGSVGElement);
document.Save(Path.Combine(OutputDir, "svg-text-builder.svg"));
}
// Add SVG text elements using the SVG Builder API in C#
// Learn more: https://docs.aspose.com/svg/net/svg-builder-api/
using (var document = new SVGDocument())
{
var builder = new SVGSVGElementBuilder();
builder.Attribute("width", "640");
builder.Attribute("height", "260");
builder.Attribute("viewBox", "0 0 640 260");
builder.AddRect(rect =>
{
rect.X(0);
rect.Y(0);
rect.Width(640);
rect.Height(260);
rect.Fill(Color.FromArgb(245, 247, 251));
});
builder.AddText(
"Aspose.SVG Builder Text",
60,
110,
34,
null,
"Arial",
null,
Color.Teal,
Color.FromArgb(0, 96, 96),
"headline",
text => text.TextAnchor(TextAnchor.Start));
builder.AddText(text =>
{
text.X(60);
text.Y(170);
text.FontSize(22);
text.FontFamily("Arial");
text.Fill(Color.FromArgb(80, 80, 80));
text.AddContent("Text content added through AddContent.");
});
builder.Build(document.RootElement as SVGSVGElement);
document.Save(Path.Combine(OutputDir, "svg-text-using-builder.svg"));
}
// Apply an SVG Gaussian blur filter using the SVG Builder API in C#
// Learn more: https://docs.aspose.com/svg/net/svg-builder-api/
using (var document = new SVGDocument("<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>", "."))
{
var builder = new SVGSVGElementBuilder();
builder.Attribute("width", "760");
builder.Attribute("height", "220");
builder.Attribute("viewBox", "0 0 760 220");
builder.AddDefs(defs => defs
.AddFilter(filter => filter
.Id("soft-blur")
.AddFeGaussianBlur(blur => blur
.StdDeviation(8, 8)
)
)
);
builder.AddRect(rect => rect
.X(30)
.Y(40)
.Width(150)
.Height(110)
.Fill(Color.FromArgb(255, 152, 0))
.Stroke(Color.FromArgb(121, 85, 72))
.StrokeWidth(3)
);
builder.AddRect(rect => rect
.X(230)
.Y(40)
.Width(150)
.Height(110)
.Fill(Color.FromArgb(255, 152, 0))
.Stroke(Color.FromArgb(121, 85, 72))
.StrokeWidth(3)
.Filter(filter => filter.FilterId("soft-blur"))
);
builder.AddCircle(circle => circle
.Cx(550)
.Cy(95)
.R(55)
.Fill(Color.Teal)
.Filter(filter => filter.FilterId("soft-blur"))
);
builder.Build(document.RootElement as SVGSVGElement);
document.Save(Path.Combine(OutputDir, "gaussian-blur-builder.svg"));
}
// Change the SVG background color using the SVG Builder API in C#
// Learn more: https://docs.aspose.com/svg/net/change-svg-background-color/
// 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("background")
.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 SVG styles with external @font-face rules using Rule Builder in C#
// 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 a simple triangle path using SVG Builder in C#
// Learn more: https://docs.aspose.com/svg/net/path-builder/
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(d => d
.M(50, 150)
.L(150, 150)
.L(100, 50)
.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, "triangle-path.svg"));
}
// Create basic SVG shapes programmatically using SVG Builder in C#
// 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 Bezier curve path using SVG Builder in C#
// Learn more: https://docs.aspose.com/svg/net/path-builder/
using (var document = new SVGDocument())
{
var builder = new SVGSVGElementBuilder();
builder.Attribute("width", "640");
builder.Attribute("height", "360");
builder.Attribute("viewBox", "0 0 640 360");
builder.AddRect(rect =>
{
rect.X(0);
rect.Y(0);
rect.Width(640);
rect.Height(360);
rect.Fill(Color.FromArgb(245, 245, 245));
});
builder.AddPath(path => path
.D(d =>
{
d.M(60, 260);
d.C(140, 80, 260, 80, 340, 260);
d.S(520, 440, 580, 160);
})
.Fill(Color.FromArgb(245, 245, 245))
.Stroke(Color.FromArgb(25, 118, 210))
.StrokeWidth(8)
);
builder.AddCircle(circle =>
{
circle.Cx(60);
circle.Cy(260);
circle.R(8);
circle.Fill(Color.FromArgb(255, 152, 0));
});
builder.Build(document.RootElement as SVGSVGElement);
document.Save(Path.Combine(OutputDir, "path-builder.svg"));
}
// Create a circular SVG clip path using SVG Builder in C#
// Learn more: https://docs.aspose.com/svg/net/svg-builder-api/
using (var document = new SVGDocument())
{
var builder = new SVGSVGElementBuilder();
builder.Attribute("width", "440");
builder.Attribute("height", "300");
builder.AddDefs(defs => defs
.AddClipPath(clipPath => clipPath
.Id("circle-window")
.ClipPathUnits(CoordinateUnits.UserSpaceOnUse)
.AddCircle(circle =>
{
circle.Cx(220);
circle.Cy(150);
circle.R(110);
})
)
);
builder.AddRect(rect =>
{
rect.X(0);
rect.Y(10);
rect.Width(440);
rect.Height(300);
rect.Fill(Color.FromArgb(245, 247, 251));
});
builder.AddRect(rect =>
{
rect.X(70);
rect.Y(60);
rect.Width(300);
rect.Height(220);
rect.Fill(Color.Teal);
rect.Attribute("clip-path", "url(#circle-window)");
});
builder.Build(document.RootElement as SVGSVGElement);
document.Save(Path.Combine(OutputDir, "clip-path-builder.svg"));
}
// Create an SVG clip path with shapes using the SVG Builder API in C#
// Learn more: https://docs.aspose.com/svg/net/svg-builder-api/
// Create a new SVG document
using (var document = new SVGDocument())
{
// Build the SVG root element with clip path and shapes
new SVGSVGElementBuilder()
.Width(600).Height(400)
.AddDefs(defs => defs
.AddClipPath(clipPath => clipPath
.Id("clip-circle")
.ClipPathUnits(CoordinateUnits.UserSpaceOnUse)
.AddCircle(circle => circle
.Cx(150).Cy(150).R(100)
)
)
)
.AddRect(rect => rect
.X(50).Y(70).Width(300).Height(200)
.Fill(Color.FromArgb(100, 181, 246))
.Stroke(Color.FromArgb(25, 118, 210))
.StrokeWidth(3)
.Attribute("clip-path", "url(#clip-circle)")
)
.AddCircle(circle => circle
.Cx(300).Cy(150).R(80)
.Fill(Color.FromArgb(255, 152, 0))
.Stroke(Color.FromArgb(156, 39, 176))
.StrokeWidth(4)
)
.AddText(text => text
.X(100).Y(350)
.Fill(Color.FromArgb(33, 150, 243))
.FontSize(24)
.FontFamily("Arial")
.AddContent("Clip Path Example")
)
.Build(document.RootElement as SVGSVGElement);
// Save the SVG document
document.Save(Path.Combine(OutputDir, "clip-path-example.svg"));
}
// Create a complex SVG with shapes, gradients, paths, 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 an SVG document from scratch using the SVG Builder API in C#
// Learn more: https://docs.aspose.com/svg/net/element-builders/
using (var svgDocument = new SVGDocument())
{
new SVGSVGElementBuilder()
.Width(420)
.Height(180)
.ViewBox(0, 0, 420, 180)
.AddRect(rect => rect
.X(20).Y(20).Width(160).Height(100)
.Fill(Color.Gold)
.Stroke(Color.DarkOrange)
.StrokeWidth(4))
.AddCircle(circle => circle
.Cx(280).Cy(70).R(50)
.Fill(Color.Teal))
.Build(svgDocument.RootElement);
svgDocument.Save(Path.Combine(OutputDir, "builder.svg"));
}
}
// Create an SVG drop shadow filter using the SVG Builder API in C#
// Learn more: https://docs.aspose.com/svg/net/svg-builder-api/
using (var document = new SVGDocument())
{
new SVGSVGElementBuilder()
.AddDefs(defs => defs
.AddFilter(filter => filter
.Id("drop-shadow")
.AddFeDropShadow(dropShadow => dropShadow
.Dx(4)
.Dy(4)
.StdDeviation(5, 5)
)
)
)
.AddRect(rect => rect
.X(50).Y(50).Width(200).Height(100)
.Fill(Color.FromArgb(255, 152, 0))
.Filter(filter => filter.FilterId("drop-shadow"))
)
.Build(document.RootElement as SVGSVGElement);
document.Save(Path.Combine(OutputDir, "drop-shadow-effect.svg"));
}
// Create styled SVG circle and text elements using the Rule Builder API in C#
// 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 curves and Bezier paths using SVG Builder in C#
// Learn more: https://docs.aspose.com/svg/net/path-builder/
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(500).Height(500)
.ViewBox(0, 0, 400, 400)
.AddPath(p => p
.D(d => d
.M(80, 210)
.Q(95, 110, 200, 200)
.T(350, 200)
)
// Sets the fill color of the path to teal
.Fill(Color.Red))
.Build(document.FirstChild as SVGSVGElement);
// Save the SVG document
document.Save(Path.Combine(OutputDir, "curve-path.svg"));
}
// Create an SVG document from scratch using the SVG Builder API in C#
// 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 linear gradient fill using the SVG Builder API in C#
// Learn more: https://docs.aspose.com/svg/net/paint-builder/
using (SVGDocument document = new SVGDocument())
{
SVGSVGElement svg = new SVGSVGElementBuilder()
.Width(400).Height(200)
.ViewBox(0, 0, 400, 200)
.AddDefs(defs => defs
.AddLinearGradient(gradient => gradient
.Id("gradientFill")
.X1(0, LengthType.Percentage).Y1(0, LengthType.Percentage)
.X2(100, LengthType.Percentage).Y2(0, LengthType.Percentage)
.AddStop(stop => stop
.Offset(0, StopUnitType.Percentage)
.Attribute("stop-color", "gold"))
.AddStop(stop => stop
.Offset(100, StopUnitType.Percentage)
.Attribute("stop-color", "teal"))))
.AddRect(rect => rect
.Rect(20, 20, 360, 160)
.Fill(paint => paint.PaintServerId("gradientFill")))
.Build(document.FirstChild as SVGSVGElement);
document.Save(Path.Combine(OutputDir, "linear-gradient-fill.svg"));
}
// Create an SVG path element with line commands using SVG Path Builder in C#
// 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 an SVG pattern with polygons and use it as a rectangle fill in C#
// 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 reusable SVG patterns and apply them to circles using Paint Builder in C#
// 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 along a path using SVG Builder in C#
// 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 circle element using SVG Builder in C#
// 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