473,407 Members | 2,326 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,407 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 5971
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

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...
1
by: rija | last post by:
Hi all, I would like to know how can I do to display print dialog so that the user can choose printer when printing word document object. Basically, I download rtf document on the HD and open...
0
by: Siv | last post by:
Hi, This is driving me nuts, I have created a custom print preview dialog, I have a toolbar at the top of the window that allows the user to select a page setup dialog and a printer dialog. All...
0
by: hoderbob | last post by:
I need to find a way to catch the parameters entered in the Print Dialog box of any Windows application and pull the number of copies the user selected. I know most printers allow the driver to...
1
by: henning | last post by:
Hello, I'm involved in the development of a set of web pages that are meant to be used on touch screens. We use ordinary HTML, PHP and javascript for the pages. I wold prefer a serverside...
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...
3
by: ARC | last post by:
Hello all, Using the following command, is there any way to get the printer dialog to open? I most packages, when you click print, it will open the printer dialog, however Access sends the...
6
by: =?Utf-8?B?QnJhc3NpY2FOaWdyYQ==?= | last post by:
Greetings, I have a web application that prints a form using PrintDocument on the server. Currently it is set up to print to the default printer (on the server). My users would like to be...
0
by: Andrew Meador | last post by:
I have implemented a printing scenario where an html file is printed using the the following code: public void PrintHtmlFile(string url) { RegistryKey IERegKey; string header = null; string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.