Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/e8a6e1cbefd7134e27eef9d434c52e6a to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/e8a6e1cbefd7134e27eef9d434c52e6a to your computer and use it in GitHub Desktop.
This Gist contains C# examples Aspose.SVG for .NET – Environment Configuration

Aspose.SVG for .NET – Configure Styles, Fonts, Runtime, and Network Services in C#

This gist collection contains C# examples from the Environment Configuration chapter of the Aspose.SVG for .NET documentation. Learn how to configure user styles, character encoding, custom fonts, JavaScript timeouts, and network error logging during SVG processing and conversion.

Topics Covered

These examples cover key aspects of configuring an SVG processing environment:

  • User Agent Service – Configure custom styles, character encoding, and font lookup folders.
  • Runtime Service – Limit JavaScript execution time during SVG processing.
  • Network Service – Intercept requests and handle external resource loading errors.
  • Custom Message Handlers – Log unreachable SVG resources with LogMessageHandler.
  • SVG Conversion Configuration – Apply environment settings when converting SVG to PDF or PNG.

How to Use These Examples

Each gist is a self-contained example that you can copy and run in your .NET development environment.

  1. Make sure you have the Aspose.SVG for .NET library installed.
  2. Find the gist that corresponds to your specific task.
  3. Copy the C# code directly from the gist.
  4. Adjust file paths, input, and output parameters as needed.
  5. Run the code to see how environment configuration works.

You can download a free trial of Aspose.SVG for .NET and use a temporary license for unrestricted access.

Prerequisites

  • .NET Platforms: .NET Framework 4.6.1+, .NET Core 2.0+, or .NET 5+.
  • Supported OS: Windows, Linux, macOS.
  • Development environment such as Visual Studio or JetBrains Rider.
  • Aspose.SVG for .NET installed via NuGet.

Resources

Aspose.SVG for .NET – Environment Configuration
// Configure SVG styles, encoding, and fonts before PDF conversion in C#
// Learn more: https://docs.aspose.com/svg/net/environment-configuration/
// Prepare SVG code and save it to a file
string code = "<svg xmlns=\"http://www.w3.org/2000/svg\">\r\n" +
" <circle cx=\"40\" cy=\"80\" r=\"30\" />\r\n" +
" <text x=\"80\" y=\"100\">Aspose.SVG</text>\r\n" +
"</svg>\r\n";
File.WriteAllText(Path.Combine(OutputDir, "user-agent.svg"), code);
// Create an instance of the Configuration class
using (Configuration configuration = new Configuration())
{
// Get the IUserAgentService
IUserAgentService userAgentService = configuration.GetService<IUserAgentService>();
// Set custom styles for <circle> and <text> elements
userAgentService.UserStyleSheet = "circle { fill:silver; }\r\n" +
"text { fill:DarkCyan; font-size:3em; }\r\n";
// Set ISO-8859-1 encoding to parse a document
userAgentService.CharSet = "ISO-8859-1";
// Set a custom font folder path
userAgentService.FontsSettings.SetFontsLookupFolder(Path.Combine(DataDir, "fonts"));
// Initialize an SVG document with the specified configuration
using (SVGDocument document = new SVGDocument(Path.Combine(OutputDir, "user-agent.svg"), configuration))
{
// Convert SVG to PDF
Converter.ConvertSVG(document, new PdfSaveOptions(), Path.Combine(OutputDir, "user-agent.pdf"));
}
}
// Limit JavaScript execution time in SVG before conversion to PNG in C#
// Learn more: https://docs.aspose.com/svg/net/environment-configuration/
// Prepare SVG code and save it to a file
string code = "<svg xmlns=\"http://www.w3.org/2000/svg\">\r\n" +
" <script> while(true) {} </script>\r\n" +
" <circle cx=\"40\" cy=\"80\" r=\"30\" />\r\n" +
"</svg>\r\n";
File.WriteAllText(Path.Combine(OutputDir, "runtime.svg"), code);
// Create an instance of the Configuration class
using (Configuration configuration = new Configuration())
{
// Limit JS execution time to 5 seconds
IRuntimeService runtimeService = configuration.GetService<IRuntimeService>();
runtimeService.JavaScriptTimeout = TimeSpan.FromSeconds(5);
// Initialize an SVG document with specified configuration
using (SVGDocument document = new SVGDocument(Path.Combine(OutputDir, "runtime.svg"), configuration))
{
// Convert SVG to PNG
Converter.ConvertSVG(document, new ImageSaveOptions(), Path.Combine(OutputDir, "runtime.png"));
}
}
// Create a custom message handler to log SVG network errors in C#
// Learn more: https://docs.aspose.com/svg/net/environment-configuration/
// Define LogMessageHandler that is derived from the MessageHandler class
public class LogMessageHandler : MessageHandler
{
private List<string> errors = new List<string>();
public List<string> ErrorMessages
{
get { return errors; }
}
// Override the Invoke() method
public override void Invoke(INetworkOperationContext context)
{
// Check whether response is OK
if (context.Response.StatusCode != HttpStatusCode.OK)
{
// Set error information
errors.Add(string.Format("File '{0}' Not Found", context.Request.RequestUri));
}
// Invoke the next message handler in the chain
Next(context);
}
}
// Log SVG network errors for external images and convert SVG to PNG in C#
// Learn more: https://docs.aspose.com/svg/net/environment-configuration/
// Prepare SVG code and save it to a file
string code = "<svg xmlns=\"http://www.w3.org/2000/svg\">\r\n" +
" <image href=\"https://docs.aspose.com/svg/images/drawing/park.jpg\" width=\"640px\" height=\"480px\" />\r\n" +
" <image href=\"https://docs.aspose.com/svg/net/missing1.svg\" width=\"400px\" height=\"300px\" />\r\n" +
" <image href=\"https://docs.aspose.com/svg/net/missing2.svg\" width=\"400px\" height=\"300px\" />\r\n" +
"</svg>\r\n";
File.WriteAllText(Path.Combine(OutputDir, "network.svg"), code);
// Create an instance of the Configuration class
using (Configuration configuration = new Configuration())
{
// Add LogMessageHandler to the chain of existing message handlers
INetworkService networkService = configuration.GetService<INetworkService>();
LogMessageHandler logHandler = new LogMessageHandler();
networkService.MessageHandlers.Add(logHandler);
// Initialize an SVG document with the specified configuration
using (SVGDocument document = new SVGDocument(Path.Combine(OutputDir, "network.svg"), configuration))
{
// Convert SVG to PNG
Converter.ConvertSVG(document, new ImageSaveOptions(), Path.Combine(OutputDir, "network.png"));
// Print the List of ErrorMessages
foreach (string errorMessage in logHandler.ErrorMessages)
{
Console.WriteLine(errorMessage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment