Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active September 29, 2020 07:29
Show Gist options
  • Save aspose-com-gists/0420a5530f73658aebea7f98ea4d705f to your computer and use it in GitHub Desktop.
Save aspose-com-gists/0420a5530f73658aebea7f98ea4d705f to your computer and use it in GitHub Desktop.
Aspose.SVG for .NET
This gist contains code snippets for Aspose.SVG for .NET API.
License svgLicense = new License();
// Pass the name of the embedded license file
svgLicense.SetLicense("Aspose.SVG.lic");
// Create an instance of CAD Metered class
Metered metered = new Metered();
// Access the setMeteredKey property and pass public and private keys as parameters
metered.SetMeteredKey("*****", "*****");
// Get metered data amount before calling API
decimal amountbefore = Metered.GetConsumptionQuantity();
// Display information
Console.WriteLine("Amount Consumed Before: " + amountbefore.ToString());
// Get metered data amount After calling API
decimal amountafter = Metered.GetConsumptionQuantity();
// Display information
Console.WriteLine("Amount Consumed After: " + amountafter.ToString());
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
string fileName = dataDir + "Aspose.SVG.lic.Lic";
License svgLicense = new License();
svgLicense.SetLicense(fileName);
string dataDir = RunExamples.GetDataDir_Data();
// Initialize license object
License svgLicense = new License();
// Load license in FileStream
FileStream myStream = new FileStream("Aspose.SVG.lic", FileMode.Open);
// Set license
svgLicense.SetLicense(myStream);
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
using (SvgRenderer renderer = new SvgRenderer())
using (XpsDevice device = new XpsDevice(dataDir + "ConvertSVGFilesUsingRenderer_out.xps"))
{
renderer.Render(device, document);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
using (var device = new ImageDevice(new ImageRenderingOptions(ImageFormat.Bmp), dataDir + "smiley_out.bmp"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
using (var device = new ImageDevice(new ImageRenderingOptions(ImageFormat.Gif), dataDir + "smiley_out.gif"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
using (var device = new ImageDevice(new ImageRenderingOptions(ImageFormat.Jpeg), dataDir + "smiley_out.jpg"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
using (var device = new ImageDevice(new ImageRenderingOptions(ImageFormat.Png), dataDir + "smiley_out.png"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
// Prepare an SVG code and save it to the file.
var code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='2' fill='red' />" +
"<circle cx='80' cy='80' r='30' stroke='black' stroke-width='1' fill='green' />" +
"</svg>";
System.IO.File.WriteAllText("example.svg", code);
// Initialize an SVG document from the file.
using (var document = new Aspose.Svg.SVGDocument("example.svg"))
{
var saveOptions = new Aspose.Svg.Saving.ImageSaveOptions(Aspose.Svg.Rendering.Image.ImageFormat.Png);
// Convert SVG to PNG
Aspose.Svg.Converters.Converter.ConvertSVG(document, saveOptions, "output.png");
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
// Prepare an SVG code and save it to the string.
var code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<rect width='300' height='100' style='fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)'/>" +
"</svg>";
// Initialize an SVG document from the string.
using (var document = new Aspose.Svg.SVGDocument(code, ""))
{
using (var device = new Aspose.Svg.Rendering.Image.ImageDevice(new Aspose.Svg.Rendering.Image.ImageRenderingOptions(Aspose.Svg.Rendering.Image.ImageFormat.Png), "output.png"))
{
//render SVG to PNG
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
using (var device = new ImageDevice(new ImageRenderingOptions(ImageFormat.Tiff), dataDir + "smiley_out.tiff"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
var options = new PdfRenderingOptions()
{
PageSetup =
{
AnyPage = new Page(new Size(500, 500))
}
};
using (var device = new PdfDevice(options, dataDir + "smiley_out.pdf"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
var options = new XpsRenderingOptions()
{
PageSetup =
{
AnyPage = new Page(new Size(500, 500))
}
};
using (var device = new XpsDevice(options, dataDir + "smiley_out.xps"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
using (var document = new SVGDocument())
{
// do some actions over the document here...
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
using (var document = new SVGDocument("<svg xmlns='http://www.w3.org/2000/svg'><circle cx='50' cy='50' r='40'/></svg>", "."))
{
// do some actions over the document here...
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
var document = new SVGDocument();
var @event = new ManualResetEvent(false);
// Subscribes to the event 'OnReadyStateChange' that will be fired once document is completely loaded
document.OnReadyStateChange += (sender, ev) =>
{
if (document.ReadyState == "complete")
{
// Sets the state of the event to signaled to unblock the main thread
@event.Set();
}
};
document.Navigate("http://www1.plurib.us/1shot/2008/circle_design/circles_single.svg");
// Blocks the current thread while the document is loading
@event.WaitOne();
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
// do some actions over the document here...
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
using (var document = new SVGDocument("http://www1.plurib.us/1shot/2008/circle_design/circles_single.svg"))
{
// do some actions over the document here...
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
var options = new XpsRenderingOptions()
{
PageSetup =
{
AnyPage = new Page(new Aspose.Svg.Drawing.Size(500, 500), new Margin(50, 50, 50, 50))
},
BackgroundColor = Color.Blue
};
using (XpsDevice device = new XpsDevice(options, dataDir + "RenderingOptions_out.xps"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
const string @namespace = "http://www.w3.org/2000/svg";
var circle = (SVGCircleElement)document.CreateElementNS(@namespace, "circle");
circle.Cx.BaseVal.Value = 50;
circle.Cy.BaseVal.Value = 50;
circle.R.BaseVal.Value = 40;
var g = document.QuerySelector("g");
g.InsertBefore(circle, g.FirstChild);
document.Save(dataDir + "SaveSVGDocument_out.svg");
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Convert();
string svgCode = "<svg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'>" +
"<style>" +
"div {" +
"color: white;" +
"font: 18px serif;" +
"height: 100%;" +
"overflow: hidden;" +
" }" +
"</style>" +
"<polygon points='5,5 195,10 185,185 10,195' />" +
"<foreignObject x='20' y='20' width='160' height='160'>" +
"<div xmlns='http://www.w3.org/1999/xhtml'>" +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit." +
"Sed mollis mollis mi ut ultricies. Nullam magna ipsum," +
"porta vel dui convallis, rutrum imperdiet eros. Aliquam" +
"erat volutpat." +
"</div>" +
"</foreignObject>" +
"</svg>";
using (var document = new SVGDocument(svgCode, "."))
{
using (var device = new ImageDevice(new ImageRenderingOptions(ImageFormat.Jpeg), dataDir + "SupportForHtmlContentInSVG_out.jpg"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Save();
SVGDocument document = new SVGDocument(dataDir + "complex.svg");
SVGSaveOptions saveOptions = new SVGSaveOptions
{
VectorizeText = true
};
document.Save(dataDir + @"vectorized_text_out.svg", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
var element = document.QuerySelector("g > :last-child");
Console.WriteLine(element.OuterHTML);
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
const string @namespace = "http://www.w3.org/2000/svg";
var circle = (SVGCircleElement)document.CreateElementNS(@namespace, "circle");
circle.Cx.BaseVal.Value = 50;
circle.Cy.BaseVal.Value = 50;
circle.R.BaseVal.Value = 40;
var g = document.QuerySelector("g");
g.AppendChild(circle);
Console.WriteLine(g.OuterHTML);
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
var svg = document.DocumentElement;
var g = svg.GetElementsByTagName("g").First() as SVGGElement;
var rect = g.FirstElementChild as SVGRectElement;
Console.WriteLine(rect.Width); // 90%
Console.WriteLine(rect.Height); // 90%
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
// Create a node iterator
using (var iterator = document.CreateNodeIterator(document, NodeFilter.SHOW_ALL, new RectFilter()))
{
Console.WriteLine((iterator.NextNode() as Element)?.OuterHTML);
Console.WriteLine((iterator.NextNode() as Element)?.OuterHTML);
Console.WriteLine((iterator.NextNode() as Element)?.OuterHTML);
}
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
var element = document.DocumentElement;
Console.WriteLine(element.TagName); // SVG
element = element.LastElementChild;
Console.WriteLine(element.TagName); // G
element = element.FirstElementChild;
Console.WriteLine(element.TagName); // RECT
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
using (var document = new SVGDocument("http://www1.plurib.us/1shot/2008/circle_design/circles_single.svg"))
{
Console.WriteLine(document.DocumentElement.OuterHTML);
}
// For complete examples and data files, please go to https://github.com/aspose-svg/Aspose.SVG-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
// Evaluate XPath expression
var xpathResult = document.Evaluate("//rect[@x='100']", document, null, XPathResultType.Any, null);
// Get the next evaluated node
Console.WriteLine((xpathResult.IterateNext() as Element)?.OuterHTML);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment