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 examples Aspose.SVG Environment Configuration

Aspose.SVG for .NET – Environment Configuration Examples

This GitHub gist repository contains practical C# code snippets from the Environment Configuration chapter of Aspose.SVG for .NET documentation. These examples demonstrate how to customize runtime behavior using various service interfaces.

Topics Covered

The examples in this repository cover key aspects of setting up an SVG processing environment:

  • User Agent Service – Customize document encoding, user stylesheet, and primary character set.
  • Runtime Service – Set timeouts to manage JavaScript execution time.
  • Network Service – Intercept web requests by implementing custom message handlers.
  • Message Handlers – Use LogMessageHandler to log unreachable resource requests.
  • Configuration Class – Central entry point to register and apply custom services.

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.

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
// Set custom user agent, styles, charset, and fonts for SVG conversion to PDF 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 a custom style parameters 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 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"));
}
}
// Сustom network message handler to log HTTP errors during SVG processing
// 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 network errors when loading external images in SVG and convert to PNG
// 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 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