Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Printing formatted text and other data with the WebBrowser control

Total Hit ( 2228)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
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 (1x1 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:

Click here to copy the following block
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:

Click here to copy the following block
AxWebBrowser.Document.parentWindow.execScript("window.print();", "JScript")

Quick and dirty!


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.