473,242 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

Printing from web form without print dialog.

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
3 3637
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
3
by: aniljain50 | last post by:
Hi All, I have a web form. let's say it has 3 fields. Employee No, Name ansd salary. Now my problem is I want to print the contents of this screen to a local printer. Note that this is client side...
0
by: LilBuh | last post by:
Hello :) this is a repost... i ve been looking for a way since last week ... i have a template html file that i modify from my code... i need to print this file without IE's Header and Footer or...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
1
by: hellohez | last post by:
We are currently looking into triggering printing of an HTML file to a designated printer in such a way that the print dialog box is not viewed. It is possible to use the print method of a default...
3
by: John Peterson | last post by:
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...
6
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if...
2
by: Brad Pears | last post by:
I have some sample code that uses the print dialog, print preview and a print direct options. If I select print preview and then click the printer icon from that, the document prints. If I...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.