473,325 Members | 2,785 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,325 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 3645
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...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.