public class SvgSymbolOptions
//Build an SVG sprite file from input SVG files with custom options
var svgDocument = new SvgDocument();
var svgSpriteBuilder = new SvgSpriteBuilder(svgDocument);
var svgSymbolOptions = new SvgSymbolOptions
{
//These are default values for options
AttributesToPreserve = new[] { "id", "viewBox", "class", "style", "fill", "stroke", "opacity", "transform" },
ElementsToPreserve = new[] { "*" },
IdForMissing = "symbol",
IdReplacementChar = '-',
IdLowerCased = true
};
foreach (var file in Directory.EnumerateFiles(@"inputs\", "*.svg"))
{
var svgDocumentToAdd = new SvgDocument(file);
var symbolId = Path.GetFileNameWithoutExtension(file);
svgSpriteBuilder.AddSymbol(svgDocumentToAdd, symbolId, svgSymbolOptions);
}
svgDocument.Save(@"sprite.svg");
//Extract symbols from an SVG sprite file to individual SVG files with custom options
var svgDocument = new SvgDocument(@"sprite.svg");
var svgSpriteBuilder = new SvgSpriteBuilder(svgDocument);
var svgSymbolOptions = new SvgSymbolOptions
{
//These are default values for options
AttributesToPreserve = new[] { "id", "viewBox", "class", "style", "fill", "stroke", "opacity", "transform" },
ElementsToPreserve = new[] { "*" },
IdForMissing = "symbol",
IdReplacementChar = '-',
IdLowerCased = true
};
var outputDirectory = @"outputs\";
foreach (var symbolId in svgSpriteBuilder.GetSymbolIds())
{
var svgDocumentToExtract = svgSpriteBuilder.ExtractSymbol(symbolId, svgSymbolOptions);
var svgFile = Path.Combine(outputDirectory, Path.ChangeExtension(symbolId, ".svg"));
Directory.CreateDirectory(outputDirectory);
svgDocumentToExtract.Save(svgFile);
}
SvgSymbolOptions | Initializes a new instance of the SvgSymbolOptions class. |
AttributesToDiscard |
Gets or sets the attributes to discard when converting <svg> tag to <symbol> tag or vice versa.
Wildcards can be used: * - Zero or more characters, ? - Exactly one character. Default value is null which means preserve all attributes set in AttributesToPreserve. Attributes that should be usually preserved on <symbol> tag:
|
AttributesToPreserve |
Gets or sets the attributes to preserve when converting <svg> tag to <symbol> tag or vice versa.
Wildcards can be used: * - Zero or more characters, ? - Exactly one character. Default value is { "id", "viewBox", "class", "style", "fill", "stroke", "opacity", "transform" }. Attributes that should be usually preserved on <symbol> tag:
|
CommentsPreserved |
Gets or sets a value indicating whether to preserve comments when converting <svg> tag to <symbol> tag or vice versa.
Default value is . |
Default | Gets the default instance of the SvgSymbolOptions class. |
ElementsToDiscard |
Gets or sets the elements to discard when converting <svg> tag to <symbol> tag or vice versa.
Wildcards can be used: * - Zero or more characters, ? - Exactly one character. Default value is null which means preserve all elements set in ElementsToPreserve. Elements that should be usually preserved inside <symbol> tag:
|
ElementsToPreserve |
Gets or sets the attributes to preserve when converting <svg> tag to <symbol> tag or vice versa.
Wildcards can be used: * - Zero or more characters, ? - Exactly one character. Default value is { "*" }. Elements that should be usually preserved inside <symbol> tag:
|
IdForMissing |
Gets or sets the value to use when id attribute is missing or is not specified when converting <svg> tag to <symbol> tag or vice versa.
When there are duplicates, this value will be incremented like "symbol-2", "symbol-3" etc. Default value is "symbol". |
IdLowerCased |
Gets or sets a value indicating whether to convert the id attribute to lowercase when converting <svg> tag to <symbol> tag or vice versa.
Default value is . |
IdPrefix |
Gets or sets the prefix to add to the id attribute when converting <svg> tag to <symbol> tag or vice versa.
Default value is null which means not to add a prefix. |
IdReplacementChar |
Gets or sets the replacement character when sanitizing the id attribute when converting <svg> tag to <symbol> tag or vice versa.
Default value is '-'. Allowed values are '-', '_', '.'. Setting any other value will disable sanitizing.
The disallowed characters will be replaced with IdReplacementChar if it was set.
|
ViewBoxOverride |
Gets or sets the override value to use for all viewBox attributes, e.g. "0 0 24 24", when converting <svg> tag to <symbol> tag or vice versa.
Default value is . |