Table of Contents

Class SvgDocument

Namespace
Svg
Assembly
Svg.dll

The class used to create and load SVG documents.

public class SvgDocument : SvgFragment, ISvgTransformable, ICloneable, ISvgNode, ISvgViewPort, ISvgBoundable, ITypeDescriptorContext, IServiceProvider
Inheritance
SvgDocument
Implements
Inherited Members
Extension Methods

Constructors

SvgDocument()

Initializes a new instance of the SvgDocument class.

public SvgDocument()

Properties

BaseUri

public Uri BaseUri { get; set; }

Property Value

Uri

DisableDtdProcessing

Skip the Dtd Processing for faster loading of svgs that have a DTD specified. For Example Adobe Illustrator svgs.

public static bool DisableDtdProcessing { get; set; }

Property Value

bool

ExternalCSSHref

Gets or sets an external Cascading Style Sheet (CSS)

public string ExternalCSSHref { get; set; }

Property Value

string

IdManager

Gets an SvgElementIdManager for this document.

protected virtual SvgElementIdManager IdManager { get; }

Property Value

SvgElementIdManager

Overflow

public override SvgOverflow Overflow { get; }

Property Value

SvgOverflow

PointsPerInch

public static int PointsPerInch { get; set; }

Property Value

int

Ppi

Gets or sets the Pixels Per Inch of the rendered image.

public int Ppi { get; set; }

Property Value

int

ResolveExternalElements

Which types of external elements, for example text definitions, are allowed to be resolved. Defaults to Local and Remote.

public static ExternalType ResolveExternalElements { get; set; }

Property Value

ExternalType

ResolveExternalImages

Which types of external images are allowed to be resolved. Defaults to Local and Remote.

public static ExternalType ResolveExternalImages { get; set; }

Property Value

ExternalType

ResolveExternalXmlEntites

Which types of XML external entities are allowed to be resolved. Defaults to None to prevent XXE.

public static ExternalType ResolveExternalXmlEntites { get; set; }

Property Value

ExternalType

SkipGdiPlusCapabilityCheck

Skip check whether the GDI+ can be loaded.

public static bool SkipGdiPlusCapabilityCheck { get; set; }

Property Value

bool

Remarks

Set to true on systems that do not support GDI+ like UWP.

X

Gets or sets the position where the left point of the svg should start.

public override SvgUnit X { get; }

Property Value

SvgUnit

Y

Gets or sets the position where the top point of the svg should start.

public override SvgUnit Y { get; }

Property Value

SvgUnit

Methods

Draw()

Renders the SvgDocument and returns the image as a Bitmap.

public virtual Bitmap Draw()

Returns

Bitmap

A Bitmap containing the rendered document.

Draw(ISvgRenderer)

Renders the SvgDocument to the specified ISvgRenderer.

public void Draw(ISvgRenderer renderer)

Parameters

renderer ISvgRenderer

The ISvgRenderer to render the document with.

Exceptions

ArgumentNullException

The renderer parameter cannot be null.

Draw(Bitmap)

Renders the SvgDocument into a given Bitmap Bitmap.

public virtual void Draw(Bitmap bitmap)

Parameters

bitmap Bitmap

Draw(Graphics)

Renders the SvgDocument to the specified Graphics.

public void Draw(Graphics graphics)

Parameters

graphics Graphics

The Graphics to be rendered to.

Exceptions

ArgumentNullException

The graphics parameter cannot be null.

Draw(Graphics, SizeF?)

Renders the SvgDocument to the specified Graphics.

public void Draw(Graphics graphics, SizeF? size)

Parameters

graphics Graphics

The Graphics to be rendered to.

size SizeF?

The SizeF to render the document. If null document is rendered at the default document size.

Exceptions

ArgumentNullException

The graphics parameter cannot be null.

Draw(int, int)

Renders the SvgDocument in given size and returns the image as a Bitmap. If one of rasterWidth and rasterHeight is zero, the image is scaled preserving aspect ratio, otherwise the aspect ratio is ignored.

public virtual Bitmap Draw(int rasterWidth, int rasterHeight)

Parameters

rasterWidth int
rasterHeight int

Returns

Bitmap

A Bitmap containing the rendered document.

EnsureSystemIsGdiPlusCapable()

Ensure that the running system is GDI capable, if not this will yield a SvgGdiPlusCannotBeLoadedException exception.

public static void EnsureSystemIsGdiPlusCapable()

FromSvg<T>(string)

Attempts to create an SVG document from the specified string data.

public static T FromSvg<T>(string svg) where T : SvgDocument, new()

Parameters

svg string

The SVG data.

Returns

T

Type Parameters

T

GetElementById(string)

Retrieves the SvgElement with the specified ID.

public virtual SvgElement GetElementById(string id)

Parameters

id string

A string containing the ID of the element to find.

Returns

SvgElement

An SvgElement of one exists with the specified ID; otherwise false.

GetElementById<TSvgElement>(string)

Retrieves the SvgElement with the specified ID.

public virtual TSvgElement GetElementById<TSvgElement>(string id) where TSvgElement : SvgElement

Parameters

id string

A string containing the ID of the element to find.

Returns

TSvgElement

An SvgElement of one exists with the specified ID; otherwise false.

Type Parameters

TSvgElement

Open(string)

Opens the document at the specified path and loads the SVG contents.

public static SvgDocument Open(string path)

Parameters

path string

A string containing the path of the file to open.

Returns

SvgDocument

An SvgDocument with the contents loaded.

Exceptions

FileNotFoundException

The document at the specified path cannot be found.

Open(XmlDocument)

Opens an SVG document from the specified XmlDocument.

public static SvgDocument Open(XmlDocument document)

Parameters

document XmlDocument

The XmlDocument containing the SVG document XML.

Returns

SvgDocument

Exceptions

ArgumentNullException

The document parameter cannot be null.

OpenAsBitmap(string)

public static Bitmap OpenAsBitmap(string path)

Parameters

path string

Returns

Bitmap

OpenAsBitmap(XmlDocument)

public static Bitmap OpenAsBitmap(XmlDocument document)

Parameters

document XmlDocument

Returns

Bitmap

Open<T>(Stream)

Attempts to open an SVG document from the specified Stream.

public static T Open<T>(Stream stream) where T : SvgDocument, new()

Parameters

stream Stream

The Stream containing the SVG document to open.

Returns

T

Type Parameters

T

Open<T>(Stream, SvgOptions)

Opens an SVG document from the specified Stream and adds the specified entities.

public static T Open<T>(Stream stream, SvgOptions svgOptions) where T : SvgDocument, new()

Parameters

stream Stream

The Stream containing the SVG document to open.

svgOptions SvgOptions

Css Style that will be applied to the Svg Document

Returns

T

Type Parameters

T

Exceptions

ArgumentNullException

The stream parameter cannot be null.

Open<T>(Stream, Dictionary<string, string>)

Opens an SVG document from the specified Stream and adds the specified entities.

[Obsolete("Use Open<T>(Stream stream, SvgOptions svgOptions)")]
public static T Open<T>(Stream stream, Dictionary<string, string> entities) where T : SvgDocument, new()

Parameters

stream Stream

The Stream containing the SVG document to open.

entities Dictionary<string, string>

Custom entity definitions.

Returns

T

Type Parameters

T

Exceptions

ArgumentNullException

The stream parameter cannot be null.

Open<T>(string)

Opens the document at the specified path and loads the SVG contents.

public static T Open<T>(string path) where T : SvgDocument, new()

Parameters

path string

A string containing the path of the file to open.

Returns

T

An SvgDocument with the contents loaded.

Type Parameters

T

Exceptions

FileNotFoundException

The document at the specified path cannot be found.

Open<T>(string, SvgOptions)

Opens the document at the specified path and loads the SVG contents.

public static T Open<T>(string path, SvgOptions svgOptions) where T : SvgDocument, new()

Parameters

path string

A string containing the path of the file to open.

svgOptions SvgOptions

A dictionary of custom entity definitions to be used when resolving XML entities within the document.

Returns

T

A SvgDocument with the contents loaded.

Type Parameters

T

Exceptions

FileNotFoundException

The document at the specified path cannot be found.

Open<T>(string, Dictionary<string, string>)

Opens the document at the specified path and loads the SVG contents.

[Obsolete("Use Open<T>(string path, SvgOptions svgOptions)")]
public static T Open<T>(string path, Dictionary<string, string> entities) where T : SvgDocument, new()

Parameters

path string

A string containing the path of the file to open.

entities Dictionary<string, string>

A dictionary of custom entity definitions to be used when resolving XML entities within the document.

Returns

T

An SvgDocument with the contents loaded.

Type Parameters

T

Exceptions

FileNotFoundException

The document at the specified path cannot be found.

Open<T>(XmlReader)

Attempts to open an SVG document from the specified XmlReader.

public static T Open<T>(XmlReader reader) where T : SvgDocument, new()

Parameters

reader XmlReader

The XmlReader containing the SVG document to open.

Returns

T

Type Parameters

T

OverwriteIdManager(SvgElementIdManager)

Overwrites the current IdManager with a custom implementation. Be careful with this: If elements have been inserted into the document before, you have to take care that the new IdManager also knows of them.

public void OverwriteIdManager(SvgElementIdManager manager)

Parameters

manager SvgElementIdManager

RasterizeDimensions(ref SizeF, int, int)

If both or one of raster height and width is not given (0), calculate that missing value from original SVG size while keeping original SVG size ratio

public virtual void RasterizeDimensions(ref SizeF size, int rasterWidth, int rasterHeight)

Parameters

size SizeF
rasterWidth int
rasterHeight int

SystemIsGdiPlusCapable()

Validate whether the system has GDI+ capabilities (non Windows related).

public static bool SystemIsGdiPlusCapable()

Returns

bool

Boolean whether the system is capable of using GDI+

Write(Stream, bool)

public void Write(Stream stream, bool useBom = true)

Parameters

stream Stream
useBom bool

Write(string, bool)

public void Write(string path, bool useBom = true)

Parameters

path string
useBom bool

Write(XmlWriter)

Write this SvgElement out using a given XmlWriter.

public override void Write(XmlWriter writer)

Parameters

writer XmlWriter

The XmlWriter to use.

Remarks

Recommendation is to create an XmlWriter by calling a factory method,
e.g. Create(Stream), as per Microsoft documentation.

However, unlike an XmlTextWriter created via 'new XmlTextWriter()',
a factory-constructed XmlWriter will not flush output until it is closed
(normally via a using statement), or unless the client explicitly calls Flush().

WriteAttributes(XmlWriter)

protected override void WriteAttributes(XmlWriter writer)

Parameters

writer XmlWriter