Environment Configuration in .NET with Aspose.HTML

In today’s digital world, creating and manipulating HTML documents is a fundamental task for many developers. Whether you’re building a web application or need to convert HTML to other formats like PDF or images, Aspose.HTML for .NET is a powerful tool to have in your toolkit. In this tutorial, we’ll explore various aspects of Aspose.HTML for .NET, including prerequisites, importing namespaces, and step-by-step examples with detailed explanations.

Prerequisites

Before we dive into using Aspose.HTML for .NET, you’ll need to ensure you have the following prerequisites in place:

  1. Visual Studio: Make sure you have Visual Studio installed on your development machine. Aspose.HTML for .NET is designed to work seamlessly with Visual Studio.

  2. Aspose.HTML for .NET: You can download the Aspose.HTML for .NET library from the website. Use the following link to access the download page: Download Aspose.HTML for .NET.

  3. Installation and License: After downloading the library, follow the installation instructions provided in the documentation. You may also need a valid license to use some of the advanced features. You can obtain a license from the Aspose website: Purchase Aspose.HTML License.

  4. Free Trial: If you want to try out Aspose.HTML before purchasing a license, you can get a free trial version from this link: Aspose.HTML Free Trial.

Now that you have the necessary prerequisites, let’s proceed to the next section where we’ll import the required namespaces.

Import Namespaces

To work with Aspose.HTML for .NET effectively, you’ll need to import the appropriate namespaces into your project. Below, we’ll list the namespaces you need for the examples we’ll cover:

using Aspose.Html;
using Aspose.Html.Configuration;
using Aspose.Html.Sandbox;
using Aspose.Html.Services;
using Aspose.Html.Saving;
using System;
using System.IO;

With these namespaces imported, you can access the functionality provided by Aspose.HTML for .NET.

Disable Scripts Execution

Let’s start with a basic example of disabling script execution within an HTML document and converting it to a PDF. Follow these steps:

  1. Create an HTML code snippet and save it to a file named “document.html.”
var code = "<span>Hello World!!</span> " +
           "<script>document.write('Have a nice day!');</script>";
System.IO.File.WriteAllText("document.html", code);
  1. Initialize the Aspose.HTML configuration, marking ‘scripts’ as an untrusted resource.
using (var configuration = new Aspose.Html.Configuration())
{
    configuration.Security |= Aspose.Html.Sandbox.Scripts;
    
    // Initialize an HTML document with the specified configuration
    using (var document = new Aspose.Html.HTMLDocument("document.html", configuration))
    {
        // Convert HTML to PDF
        Aspose.Html.Converters.Converter.ConvertHTML(document, new Aspose.Html.Saving.PdfSaveOptions(), "output.pdf");
    }
}

In this example, we’ve prevented the execution of scripts within the HTML document, ensuring security while converting it to a PDF. Now, let’s move on to the next example.

Specify User Stylesheet

Sometimes, you may want to apply custom styles to elements within an HTML document. Here’s how you can do it using Aspose.HTML for .NET:

  1. Create an HTML code snippet and save it to a file named “document.html.”
var code = @"<span>Hello World!!!</span>";
System.IO.File.WriteAllText("document.html", code);
  1. Set a custom color for the <span> element using a user stylesheet.
using (var configuration = new Aspose.Html.Configuration())
{
    var userAgent = configuration.GetService<Aspose.Html.Services.IUserAgentService>();
    userAgent.UserStyleSheet = "span { color: green; }";
    
    // Initialize an HTML document with the specified configuration
    using (var document = new Aspose.Html.HTMLDocument("document.html", configuration))
    {
        // Convert HTML to PDF
        Aspose.Html.Converters.Converter.ConvertHTML(document, new Aspose.Html.Saving.PdfSaveOptions(), "output.pdf");
    }
}

In this example, we’ve applied a custom style to the <span> element, setting its text color to green. Aspose.HTML for .NET allows you to manipulate styles with ease.

JavaScript Execution Timeout

When dealing with potentially time-consuming JavaScript code, it’s essential to set a timeout to prevent indefinite execution. Here’s how you can do it:

  1. Create an HTML code snippet with an endless loop and save it to a file named “document.html.”
var code = @"<script>while(true){}</script>";
System.IO.File.WriteAllText("document.html", code);
  1. Set a JavaScript execution timeout to 10 seconds.
using (var configuration = new Aspose.Html.Configuration())
{
    var runtime = configuration.GetService<Aspose.Html.Services.IRuntimeService>();
    runtime.JavaScriptTimeout = TimeSpan.FromSeconds(10);
    
    // Initialize an HTML document with the specified configuration
    using (var document = new Aspose.Html.HTMLDocument("document.html", configuration))
    {
        // Wait until all scripts are finished/canceled and convert HTML to PNG
        Aspose.Html.Converters.Converter.ConvertHTML(document, new Aspose.Html.Saving.ImageSaveOptions(), "output.png");
    }
}

In this example, we’ve limited the JavaScript execution time to 10 seconds, ensuring that the script does not run indefinitely, which could potentially cause performance issues.

Custom Message Handler

Sometimes, you may need to handle error messages or missing resources when loading an HTML document. Here’s an example of how to create a custom message handler:

  1. Create an HTML code snippet with a missing image file reference and save it to a file named “document.html.”
var code = @"<img src='missing.jpg'>";
System.IO.File.WriteAllText("document.html", code);
  1. Add an error message handler to the network service to log failed requests.
using (var configuration = new Aspose.Html.Configuration())
{
    var network = configuration.GetService<Aspose.Html.Services.INetworkService>();
    network.MessageHandlers.Add(new LogMessageHandler());
    
    // Initialize an HTML document with the specified configuration
    // During the document loading, the application will try to load the image, and we will see the result of this operation in the console.
    using (var document = new Aspose.Html.HTMLDocument("document.html", configuration))
    {
        // Convert HTML to PNG
        Aspose.Html.Converters.Converter.ConvertHTML(document, new Aspose.Html.Saving.ImageSaveOptions(), "output.png");
    }
}

In this example, we’ve added a custom message handler (LogMessageHandler) to log information about failed requests. This can be particularly useful for debugging and handling missing resources gracefully.

Conclusion

Aspose.HTML for .NET is a versatile library that empowers developers to work with HTML documents efficiently. In this tutorial, we’ve covered essential concepts and provided step-by-step examples for common tasks, including script management, stylesheet customization, JavaScript execution control, and custom message handling.

By following the steps outlined in this tutorial, you can harness the power of Aspose.HTML for .NET to create, manipulate, and convert HTML documents in your .NET applications with confidence.

FAQ’s

Q1: Can I use Aspose.HTML for .NET without purchasing a license?

A1: Yes, you can try Aspose.HTML for .NET with a free trial version, but some advanced features may require a valid license.

Q2: How can I obtain a license for Aspose.HTML for .NET?

A2: You can purchase a license from the Aspose website: Purchase Aspose.HTML License.

Q3: What formats can I convert HTML documents to using Aspose.HTML for .NET?

A3: Aspose.HTML for .NET supports conversion to various formats, including PDF, images, and more.

Q4: Is there a community or support forum for Aspose.HTML for .NET?

A4: Yes, you can find help and support on the Aspose forums: Aspose.HTML Support Forum.

Q5: Does Aspose.HTML for .NET provide documentation and tutorials?

A5: Yes, you can access the documentation here: Aspose.HTML for .NET Documentation.