Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
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_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
You can’t perform that action at this time.