Content

DotNetBrowser displays the content of various types, for example, images, video, PDF, Flash, and other, but in most cases you will display the HTML content of a web page. This guide shows how to access web page content, get the currently selected text, find some text on a web page, save the web page as a file or a set of files, and other actions.

Content size

By default, the IBrowser size is empty. Many web pages rely on this size and require it not to be empty. Otherwise, the layout of the DOM document cannot be parsed and displayed at all.

If you do not need to display the content of a loaded web page, but the page should “think” it is loaded in an IBrowser with a non-empty size, set the size programmatically using the IBrowser.Size property.

Accessing HTML

To get a string that represents HTML of an IFrame, use the IFrame.Html property.

It is recommended to use this property only when the whole web page or a particularIFrame is loaded completely. Otherwise, you may receive an incomplete HTML or an empty string.

The following example demonstrates how to print HTML of the currently loaded main IFrame:

Console.WriteLine(browser.MainFrame?.Html);

Accessing selection

To get selection on the currently loaded IFrame as HTML, use the IFrame.SelectedHtml property:

string selectionAsHtml = frame.SelectedHtml;

If you only need to get the selected text without the selection’s HTML, use the IFrame.SelectedText property as shown in the code sample below:

string selectionAsText = frame.SelectedText;

Finding text

DotNetBrowser allows you to find text on the currently loaded web page and highlight all the matches.

DotNetBrowser searches text only through the visible content of the loaded web pages which have non-empty size.

To perform the search on the currently loaded web page, use the ITextFinder interface:

ITextFinder textFinder = browser.TextFinder;

The following example demonstrates how to find the “text” on the currently loaded web page with the specified search parameters and wait until the search is completed:


private const string Html = "<html><body><p>Find me</p><p>Find me</p></body></html>";

public static void Main()
{
    try
    {
        EngineOptions options = new EngineOptions.Builder().Build();
        using (IEngine engine = EngineFactory.Create(options))
        {
            Console.WriteLine("Engine created");

            using (IBrowser browser = engine.CreateBrowser())
            {
                Console.WriteLine("Browser created");
                browser.Size = new Size(700, 500);
                browser.MainFrame.LoadHtml(Html).Wait();

                Thread.Sleep(2000);
                // Find text from the beginning of the loaded web page.
                string searchText = "find me";
                int requestId = 0;
                IHandler<FindResultReceivedParameters> intermediateResultsHandler =
                    new Handler<FindResultReceivedParameters>(ProcessSearchResults);
                FindResult findResult;

                Console.WriteLine("Find text (1/2)");
                
                findResult = browser.TextFinder
                                    .Find(searchText, null, intermediateResultsHandler)
                                    .Result;
                Console.Out.WriteLine(FormatFindResult(findResult));

                Console.WriteLine("Find text (2/2)");
                findResult = browser.TextFinder
                                    .Find(searchText, null, intermediateResultsHandler)
                                    .Result;
                Console.Out.WriteLine(FormatFindResult(findResult));
                browser.TextFinder.StopFinding();
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
    Console.WriteLine("Press any key to terminate...");
    Console.ReadKey();
}

private static string FormatFindResult(FindResult findResult)
{
    return $"Find Result: {findResult.SelectedMatch}/{findResult.NumberOfMatches}";
}

private static void ProcessSearchResults(FindResultReceivedParameters args)
{
    FindResult result = args.FindResult;

    if (args.IsSearchFinished)
    {
        Console.Out.WriteLine("Found: "
                                + result.SelectedMatch
                                + "/"
                                + result.NumberOfMatches);
    }
    else
    {
        Console.Out.WriteLine("Search in progress... Found "
                                + result.SelectedMatch
                                + "/"
                                + result.NumberOfMatches);
    }
}

The search matches are written down as shown below:

Find Text Highlights

To clear the highlighted search results on a web page and cancel the search, use the ITextFinder.StopFinding() method. The StopFindAction parameter of this method is an action to be applied to the current selection on the web page. By default, this is StopFindAction.ClearSelection.

For example:

textFinder.StopFinding();

Saving a web page

DotNetBrowser allows to save a web page as a file or a set of files. It is done using the IBrowser.SaveWebPage(string, string, SavePageType) method which accepts these parameters:

  • A string with a name of the target file;
  • A string with the directory path to save page resources;
  • A type of the save operation (SavePageType).

For example:

string filePath = Path.GetFullPath("SavedPages\\index.html");
string dirPath = Path.GetFullPath("SavedPages\\resources");
Directory.CreateDirectory(dirPath);

if (browser.SaveWebPage(filePath, dirPath, SavePageType.CompletePage)) {
    Console.WriteLine("The web page has been saved to " + filePath);
} else {
    Console.WriteLine("Failed to save the web page to " + filePath);
}

Taking bitmap of a web page

The library allows you to take a bitmap that contains the pixels of the currently loaded web page. The bitmap size equals to the size of the IBrowser instance where the web page is loaded.

To get an image of a web page, we recommend performing the following actions:

  1. Resize the IBrowser instance to the required dimension (for example, 500x500).
  2. Load a web page and wait until the main frame of the web page is loaded completely.
  3. Take the bitmap of the currently loaded web page.

The following example demonstrates how to do that:

// 1. Resize browser to the required dimension.
browser.Size = new Size(500, 500);

// 2. Load the required web page and wait until it is loaded completely.
Console.WriteLine("Loading http://www.teamdev.com/dotnetbrowser");
browser.Navigation.LoadUrl("http://www.teamdev.com/dotnetbrowser").Wait();

// 3. Take the bitmap of the currently loaded web page. Its size will be 
// equal to the current browser's size.
DotNetBrowser.Ui.Bitmap image = browser.TakeImage();
Console.WriteLine("Browser image taken");

The received bitmap can be converted to a .NET System.Drawing.Bitmap for further manipulations, for example, saving to a PNG file:

public static System.Drawing.Bitmap ToBitmap(DotNetBrowser.Ui.Bitmap bitmap)
{
    int width = (int) bitmap.Size.Width;
    int height = (int) bitmap.Size.Height;

    byte[] data = bitmap.Pixels.ToArray();
    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width,
                            height,
                            PixelFormat.Format32bppRgb);
    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                                      ImageLockMode.WriteOnly, bmp.PixelFormat);

    Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
    bmp.UnlockBits(bmpData);
    return bmp;
}

After that the bitmap can be saved to a file:

// 4. Convert the bitmap to the required format and save it.
Bitmap bitmap = ToBitmap(image);
bitmap.Save("screenshot.png", ImageFormat.Png);
Console.WriteLine("Browser image saved");

Settings

Using IBrowserSettings you can configure different content settings for a single IBrowser instance. These are some of the actions available:

  • disable images, plugins, JavaScript, local storage, application cache;
  • allow running insecure content or allow JavaScript access clipboard.

You can find the list of other supported settings in API specification.

Default encoding

To configure the default text encoding on a web page, use the code sample below:

browser.Settings.DefaultEncoding = "UTF-8";

JavaScript

By default, JavaScript is enabled on a web page. If you need to disable JavaScript, use the code sample below:

browser.Settings.JavaScriptEnabled = false;

Images

If you do not need to display images on a web page to reduce the network traffic, you can disable them using the code sample below:

browser.Settings.ImagesEnabled = false;

Plugins

You can filter the installed plugins. If you need to disable all plugins on a web page, use the following approach:

browser.Settings.PluginsEnabled = false;

Local storage

By default, local WebStorage is enabled. To disable it and prevent JavaScript from storing data in the local storage, use the following setting:

browser.Settings.LocalStorageEnabled = false;

Scrollbars

You may want to hide scrollbars on a web page when taking bitmap of a web page or inside Kiosk applications. Use the following setting:

browser.Settings.ScrollbarsHidden = true;

Once you call this method, the web pages loaded in the IBrowser instance does not display scrollbars anymore.

Web storage

DotNetBrowser allows to access and work with JavaScript Web Storage that provides mechanisms to store key/value pairs in more intuitive way than using cookies.

There are two types of the Web Storage:

  • sessionStorage maintains a separate storage area for each given origin that is available during the page session;
  • localStorage does the same thing but persists even if the browser is closed and reopened.

To access the session and local Web Storage, use the following code sample:

IWebStorage sessionStorage = frame.SessionStorage;
IWebStorage localStorage = frame.LocalStorage;

The localStorage can be disabled. In this case, the IWebStorage methods throw the WebStorageSecurityException.

Code sample below demonstrates how to place an item to the localStorage using DotNetBrowser WebStorage API:

localStorage["Name"] = "Tom";

You can access the inserted item in JavaScript using the following code:

window.localStorage.Name; // <- Returns "Tom"

Editor commands

Editor commands are the commands for text manipulation when editing in a text field, text area, or WYSIWYG editor. Also, these commands include more common commands like Cut, Copy, Paste, SelectAll, Undo, Redo which can be executed for the frame on the loaded web page.

The editor commands can be executed as shown in the code sample below:

// Inserts "TeamDev DotNetBrowser" into the currently selected text field on the web page.
browser.MainFrame?.Execute(EditorCommand.InsertText("TeamDev DotNetBrowser"));

WebSockets

DotNetBrowser supports web pages that use WebSockets technology:

WebSockets Support

Go Top