By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

Printing from web form without print dialog.

P: n/a
Hello all!

I'm at my wits end trying to search for what I assumed to be a relatively
straightforward task. I have a Web application written in C#, and I have a
button on the form that I want to print the current contents of the browser
without bringing up the print dialog.

At first I thought it was a simple matter to have the button's "onclick"
attribute set to "window.print();". However, that always brings up the
print dialog box. There doesn't seem to be any parameters to window.print()
to suppress the dialog and just use the defaults.

In essence, I'd like to mimic the behavior of the browser's (IE, in my case)
behavior when the print button is clicked.

Is there any way to do this?

Thanks!

John Peterson
Jan 28 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I don't believe there is. Methods like the window.print() method simply
trigger the browser's print method, in other words, calling window.print()
is simply an indirect method to the browser, so whether a dialog shows up or
not is dependent on the browser (and maybe it's settings, if applicable),
not your code. I believe this is a security related thing (so that nobody
makes a web page that sends a thousand unwanted pages to your printer or
something) as well as something to make code more cross-browser compatible.
If you were simply looking to have the print settings set to certain
settings, you could do something like have the button pop up an
window.alert() asking the user to change their print settings before it
calls the window.print(). Hopefully this clears things up, and maybe gives
you a few ideas to accomplish what you were looking to do.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"John Peterson" <j0***@comcast.net> wrote in message
news:un*************@TK2MSFTNGP14.phx.gbl...
Hello all!

I'm at my wits end trying to search for what I assumed to be a relatively
straightforward task. I have a Web application written in C#, and I have
a button on the form that I want to print the current contents of the
browser without bringing up the print dialog.

At first I thought it was a simple matter to have the button's "onclick"
attribute set to "window.print();". However, that always brings up the
print dialog box. There doesn't seem to be any parameters to
window.print() to suppress the dialog and just use the defaults.

In essence, I'd like to mimic the behavior of the browser's (IE, in my
case) behavior when the print button is clicked.

Is there any way to do this?

Thanks!

John Peterson

Jan 28 '06 #2

P: n/a
Thanks Nathan. Understood completely about the security-related concerns
with just printing "blindly". I've seen examples of non-Web applications
"invoking" a browser and printing without the dialog. For example:

http://ryanfarley.com/blog/archive/2004/12/23/1330.aspx

However, the objects in use don't seem to be available to Web applications.
I wish there was some way to get a "handle" on the "current" web browser
control from within client script and do something similar. But I think
I'll have to live with the dialog box. :-(
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:un****************@TK2MSFTNGP10.phx.gbl...
I don't believe there is. Methods like the window.print() method simply
trigger the browser's print method, in other words, calling window.print()
is simply an indirect method to the browser, so whether a dialog shows up
or not is dependent on the browser (and maybe it's settings, if
applicable), not your code. I believe this is a security related thing (so
that nobody makes a web page that sends a thousand unwanted pages to your
printer or something) as well as something to make code more cross-browser
compatible. If you were simply looking to have the print settings set to
certain settings, you could do something like have the button pop up an
window.alert() asking the user to change their print settings before it
calls the window.print(). Hopefully this clears things up, and maybe gives
you a few ideas to accomplish what you were looking to do.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"John Peterson" <j0***@comcast.net> wrote in message
news:un*************@TK2MSFTNGP14.phx.gbl...
Hello all!

I'm at my wits end trying to search for what I assumed to be a relatively
straightforward task. I have a Web application written in C#, and I have
a button on the form that I want to print the current contents of the
browser without bringing up the print dialog.

At first I thought it was a simple matter to have the button's "onclick"
attribute set to "window.print();". However, that always brings up the
print dialog box. There doesn't seem to be any parameters to
window.print() to suppress the dialog and just use the defaults.

In essence, I'd like to mimic the behavior of the browser's (IE, in my
case) behavior when the print button is clicked.

Is there any way to do this?

Thanks!

John Peterson


Jan 28 '06 #3

P: n/a
I have been working on a project to print sheets in a unattended
automated fashion. I found the below code (section 3) on the internet.
I works great, but with limitations. It used the defaults of IE
(Default system printer, footers, headers, etc.). The problem I am
having is trying to catch errors/exceptions. The code seems to just
keep going. Anybody with any ideas on how to catch errors.
To print to different printers, you have to change the system default
printer (see below section 1).
To change a footer, you have to modify a reg key (see below section 2).

************************************************** ******************************
Get and Set printer
Public Function GetDefaultSystemPrinter() As String
'Get the system default printer
Try
Dim pd As New Printing.PrintDocument
Return pd.PrinterSettings.PrinterName
Catch ex As Exception

End Try

End Function

http://www.planet-source-code.com/URLSEO/vb/scripts/ShowCode!asp/txtCodeId!3053/lngWid!10/anyname.htm

************************************************** ******************************
Footer Code
Public Sub SetIEFooter(ByVal NewFooter As String)
'MS Article ID : 311280
'unable to set CurrentUser RegKey with Service because service
does not know who is current user
Try
Dim strKey As String = "Software\Microsoft\Internet
Explorer\PageSetup"
Dim bolWritable As Boolean = True
Dim strName As String = "footer"
Dim oValue As Object = NewFooter
'original footer value = &u&b&d
Dim oKey As RegistryKey =
Registry.CurrentUser.OpenSubKey(strKey, bolWritable)
oKey.SetValue(strName, oValue)
oKey.Close()
Catch ex As Exception
Dim strErrorMsg As String
strErrorMsg = "Error in SetIEFooter-PrintHTML." & vbCrLf &
ex.ToString
'add LogMessage here
MessageBox.Show(strErrorMsg)
Dim clsEmail As New Email
clsEmail.EmailSend(strErrorEmailName & strEmailDomian,
SystemInformation.ComputerName & strEmailDomian, _
"An Error Message From " &
Application.ProductName, strErrorMsg)

End Try

End Sub

************************************************** ******************************
Browser print code
Public Sub LoadBrowser(ByVal strFileURL As String)
Try
'Create a WebBrowser instance.
Dim webBrowserForPrinting As New WebBrowser()

'PrintInProgress = True
Printing = "True"

'Add an event handler that prints the document after it
loads.
AddHandler webBrowserForPrinting.DocumentCompleted, New _
WebBrowserDocumentCompletedEventHandler(AddressOf
PrintDocument)

'Set the Url property to load the document.
webBrowserForPrinting.Url = New Uri(strFileURL)

Catch ex As Exception
Dim strErrorMsg As String
strErrorMsg = "Error in LoadBrowser-PrintHTML." & vbCrLf &
ex.ToString
Printing = "Error in LoadBrowser-PrintHTML." & vbCrLf &
ex.Message
'add LogMessage here
MessageBox.Show(strErrorMsg)
Dim clsEmail As New Email
clsEmail.EmailSend(strErrorEmailName & strEmailDomian,
SystemInformation.ComputerName & strEmailDomian, _
"An Error Message From " &
Application.ProductName, strErrorMsg)

End Try

End Sub
**************
Public Sub PrintDocument(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs)

Try
If Microsoft.VisualBasic.Left(Printing, 5) = "Error" Then

Else
Dim webBrowserForPrinting As WebBrowser = CType(sender,
WebBrowser)

'Print the document now that it is fully loaded.
webBrowserForPrinting.Print()

'Dispose the WebBrowser now that the task is complete.
webBrowserForPrinting.Dispose()

Printing = "False"
End If

Catch ex As Exception
Dim strErrorMsg As String
strErrorMsg = "Error in PrintDocument-PrintHTML." & vbCrLf
& ex.ToString
Printing = "Error in PrintDocument-PrintHTML." & vbCrLf &
ex.Message
'add LogMessage here
MessageBox.Show(strErrorMsg)
Dim clsEmail As New Email
clsEmail.EmailSend(strErrorEmailName & strEmailDomian,
SystemInformation.ComputerName & strEmailDomian, _
"An Error Message From " &
Application.ProductName, strErrorMsg)
End Try

End Sub

Feb 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.