Printing formatted text and other data with the WebBrowser control

Printing formatted text and other data with the WebBrowser control

Printing con be quite difficult in VB.NET (or any other .NET language), especially if you not only have to print plain text, but also tables, images, bulleted lists, text with different fonts, styles, colors and any other type of formatted data. Here’s a tip that can make your life much easier, in many cases.

The trick is to save your data in HTML format, and then use the Microsoft Web Browser control to print it. Creating tables, lists and formatted text in HTML is very easy, as it is just plain text. Once you have the HTML string with the content to print, save it to a temporary .htm file, by using the {{C491 SaveTextFile}} routine, and by getting a temporary file name, as follows:

Imports System.IO…Dim tempFile As String = Path.ChangeExtension(Path.GetTempFileName(), "htm")SaveTextFile(tempFile, htmlData)

Now you need a Web Browser control on your form. This is a COM component, and you have to create a .NET wrapper assembly for it before being able to use it. This is done by adding a reference to the ShDocVw.dll file (located under the System32 directory) from the Customize Toolbox dialog. Once the control is available from the toolbox, drag&drop it on your form, and make it as small as possible (1×1 pixels), or put it out-of-sight, so that it is not visible at runtime. (Note: you can’t just set its Visible property to False, this won’t allow you to print its content).

At this point you can load the HTML file into the Web Browser control:

AxWebBrowser.Navigate(tempFile)

And finally you can print your formatted data by executing a JavaScript piece of code that opens the Print dialog, from which you can choose the printer to use, the number of copies, and other options:

AxWebBrowser.Document.parentWindow.execScript("window.print();", "JScript")

Quick and dirty!

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as