Thursday, June 26, 2008

How can I print a web page?

Some browsers support Javascript's Window.Print() method. You can use it in the click event of a control, say a button.

<input type="button" value="Print this window" onClick="Window.Print()"/>

This will bring up a Print Dialog which is good thing since it will allow you make up your mind whether you really want to print; allows you to see if Printer is turned on, etc.

Another way would be to place a Web Browser Control on a windows form. Use the WebBrowser's navigate() method to go to the page you want at form load. Then use the click event of a button to Print.

Ofcourse you need to add a PrintDocument control from Toolbox to your form.

Code Follows:

Imports System.Windows.Forms.WebBrowser

Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click


'The above will print the Form1

End Sub

End Class

No comments: