Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
The setting of environment configuration is used for various purposes. For example, when you develop an application, you will definitely demand some configuration that can range from runtime service or handle any web requests from the application to injecting custom themes.
In this guide, you will learn how to create various configurations and adapt them to the different environments where the application runs. This can be a custom theme, a runtime service or a web request network service.
The Aspose.Svg.Services namespace contains a set of interfaces for separated services implementations. This article considers different types of environment configuration services such as User Agent Service, Runtime Service, and Network Service. Aspose.SVG for .NET provides the Configuration class that can be used to set the environment where the application is running.
You can download the complete examples and data files from GitHub. You find out about downloading from GitHub and running examples from the How to Run the Examples section.
The User Agent Service allows you to specify a custom user stylesheet, a primary character set for the document, language and fonts settings. You can select your custom style information for a particular document and provide as little or as many environment configuration changes as needed.
The IUserAgentService interface describes a user agent environment.
The UserStyleSheet property of the IUserAgentService interface allows specifying style information for a particular document;
The CharSet property sets the primary character-set for a document.
To parse and display an SVG document correctly, the application must know what encoding is using. If the character encoding is not directly specified in the header of the document, Aspose.SVG uses UTF-8, which is defined as the default. However, if you are sure that your SVG document is written using different from UTF-8 encoding, you can specify it manually, as shown in the example above.
The FontsSettings property is used for the configuration of fonts handling. When you need to use the custom fonts instead of the fonts installed on the OS, you can set the path to your custom folder, as shown in the following code snippet.
The CSSEngineMode property gets or sets the mode in which CSS engine works.
The Language property specifies the primary language for the element’s contents and for any of the element’s attributes that contain text.
Consider an example that illustrates UserStyleSheet, CharSet and FontsSettings properties applying:
1using System.IO;
2using Aspose.Svg;
3using Aspose.Svg.Services;
4using Aspose.Svg.Converters;
5using Aspose.Svg.Saving; 1// Set custom user agent, styles, charset, and fonts for SVG conversion to PDF in C#
2
3// Prepare SVG code and save it to a file
4string code = "<svg xmlns=\"http://www.w3.org/2000/svg\">\r\n" +
5 " <circle cx=\"40\" cy=\"80\" r=\"30\" />\r\n" +
6 " <text x=\"80\" y=\"100\">Aspose.SVG</text>\r\n" +
7 "</svg>\r\n";
8
9File.WriteAllText(Path.Combine(OutputDir, "user-agent.svg"), code);
10
11// Create an instance of the Configuration class
12using (Configuration configuration = new Configuration())
13{
14 // Get the IUserAgentService
15 IUserAgentService userAgentService = configuration.GetService<IUserAgentService>();
16
17 // Set a custom style parameters for <circle> and <text> elements
18 userAgentService.UserStyleSheet = "circle { fill:silver; }\r\n" +
19 "text { fill:DarkCyan; font-size:3em; }\r\n";
20
21 // Set ISO-8859-1 encoding to parse a document
22 userAgentService.CharSet = "ISO-8859-1";
23
24 // Set a custom font folder path
25 userAgentService.FontsSettings.SetFontsLookupFolder(Path.Combine(DataDir + "fonts"));
26
27 // Initialize an SVG document with specified configuration
28 using (SVGDocument document = new SVGDocument(Path.Combine(OutputDir, "user-agent.svg"), configuration))
29 {
30 // Convert SVG to PDF
31 Converter.ConvertSVG(document, new PdfSaveOptions(), Path.Combine(OutputDir, "user-agent.pdf"));
32 }
33}The figure illustrates the result of User Agent Service applying (b) to the source “user-agent.svg” file (a).

When planning to run your application, you might require a runtime service configuration. This service gives you control over the lifetime of the internal processes. For instance, using IRuntimeService you can specify timeouts for JavaScripts. It is important to have such a timeout in case if a script contains an endless loop. The next code snippet demonstrates how to use timeouts.
1using System.IO;
2using Aspose.Svg;
3using Aspose.Svg.Services;
4using Aspose.Svg.Converters;
5using Aspose.Svg.Saving; 1// Limit JavaScript execution time in SVG before conversion to PNG in C#
2
3// Prepare SVG code and save it to a file
4string code = "<svg xmlns=\"http://www.w3.org/2000/svg\">\r\n" +
5 " <script> while(true) {} </script>\r\n" +
6 " <circle cx=\"40\" cy=\"80\" r=\"30\" />\r\n" +
7 "</svg>\r\n";
8
9File.WriteAllText(Path.Combine(OutputDir, "runtime.svg"), code);
10
11// Create an instance of the Configuration class
12using (Configuration configuration = new Configuration())
13{
14 // Limit JS execution time to 5 seconds
15 IRuntimeService runtimeService = configuration.GetService<IRuntimeService>();
16 runtimeService.JavaScriptTimeout = TimeSpan.FromSeconds(5);
17
18 // Initialize an SVG document with specified configuration
19 using (SVGDocument document = new SVGDocument(Path.Combine(OutputDir, "runtime.svg"), configuration))
20 {
21 // Convert SVG to PNG
22 Converter.ConvertSVG(document, new ImageSaveOptions(), Path.Combine(OutputDir, "runtime.png"));
23 }
24}The JavaScriptTimeout property sets TimeSpan, which limits JavaScript execution time. If the script is executed longer than provided TimeSpan, it will be cancelled. The default value is 1 minute.
Modern network environments generate a significant amount of security events and log data via network routers and switches, servers, anti-malware systems, and so on.
Aspose.SVG for .Net offers the INetworkService that is envisioned as a solution to help manage and analyze all this information. Service allows you to control all incoming/outcoming traffic and implement your custom message handlers. It can be used for different purposes, such as creating a custom caching mechanism, tracing/logging request messages, etc.
Aspose.SVG for .NET offers functionality for custom message handlers creating. Let’s develop a simple custom handler that logs information about unreachable resources. Take the following steps:
The following example demonstrates how to create LogMessageHandler to log information about unreachable resources.
1using Aspose.Svg.Net;
2using System.Collections.Generic;
3using System.Net; 1// Сustom network message handler to log HTTP errors during SVG processing
2
3// Define LogMessageHandler that is derived from the MessageHandler class
4public class LogMessageHandler : MessageHandler
5{
6 private List<string> errors = new List<string>();
7
8 public List<string> ErrorMessages
9 {
10 get { return errors; }
11 }
12
13 // Override the Invoke() method
14 public override void Invoke(INetworkOperationContext context)
15 {
16 // Check whether response is OK
17 if (context.Response.StatusCode != HttpStatusCode.OK)
18 {
19 // Set error information
20 errors.Add(string.Format("File '{0}' Not Found", context.Request.RequestUri));
21 }
22
23 // Invoke the next message handler in the chain
24 Next(context);
25 }
26}For more information about custom message handlers creation, please see the chapter Message Handlers.
The following example demonstrates how to use the LogMessageHandler class for logging information about unreachable resources.
1using System.IO;
2using Aspose.Svg;
3using Aspose.Svg.Services;
4using Aspose.Svg.Converters;
5using Aspose.Svg.Saving;
6using Aspose.Svg.Net; 1// Log network errors when loading external images in SVG and convert to PNG
2
3// Prepare SVG code and save it to a file
4string code = "<svg xmlns=\"http://www.w3.org/2000/svg\">\r\n" +
5 " <image href=\"https://docs.aspose.com/svg/images/drawing/park.jpg\" width=\"640px\" height=\"480px\" />\r\n" +
6 " <image href=\"https://docs.aspose.com/svg/net/missing1.svg\" width=\"400px\" height=\"300px\" />\r\n" +
7 " <image href=\"https://docs.aspose.com/svg/net/missing2.svg\" width=\"400px\" height=\"300px\" />\r\n" +
8 "</svg>\r\n";
9
10File.WriteAllText(Path.Combine(OutputDir, "network.svg"), code);
11
12// Create an instance of the Configuration class
13using (Configuration configuration = new Configuration())
14{
15 // Add LogMessageHandler to the chain of existing message handlers
16 INetworkService networkService = configuration.GetService<INetworkService>();
17
18 LogMessageHandler logHandler = new LogMessageHandler();
19 networkService.MessageHandlers.Add(logHandler);
20
21 // Initialize an SVG document with specified configuration
22 using (SVGDocument document = new SVGDocument(Path.Combine(OutputDir, "network.svg"), configuration))
23 {
24 // Convert SVG to PNG
25 Converter.ConvertSVG(document, new ImageSaveOptions(), Path.Combine(OutputDir, "network.png"));
26
27 // Print the List of ErrorMessages
28 foreach (string errorMessage in logHandler.ErrorMessages)
29 {
30 Console.WriteLine(errorMessage);
31 }
32 }
33}After the example run:
ErrorMessages will be printed:File 'https://docs.aspose.com/svg/net/missing1.svg' Not FoundFile 'https://docs.aspose.com/svg/net/missing2.svg' Not Found
You can download the complete examples and data files from GitHub. About downloading from GitHub and running examples, you find out from the How to Run the Examples section.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.