Results 1 to 7 of 7

Thread: *** RESOLVED *** Disable Print in WebBrowser control

  1. #1

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    Resolved *** RESOLVED *** Disable Print in WebBrowser control

    I have been trying to figure out how to disable print functionality in the WebBrowser control.

    Just a note - I am very new to C#. I have pretty heavy experience in VB6 and Java though.

    Some pages I will be loading have javascript in them that brings up the print dialog. This application is simply "scraping" the page contents and moving on to the next page in the list the user entered. When the print dialog displays, the app waits for the user response and does not continue to the next page. In addition, if the user enters 10,000 pages to navigate, I don't want them to have to click cancel on all 10,000, should each one call print through javascript.

    Code:
    <script language="JavaScript">
    window.print();
    </script>
    I have tried just about everything I can think of without any luck.

    What I have tried:
    1. Extending the WebBrowser control and overriding the print() method. The print method can not be overloaded according to my compiler...

    2. Using an older version of the WebBrowser control and setting the Silent property to true.

    3. Stripping the print() method from the page before it gets to the browser via the navigating/navigated events. The problem with this is that some pages/parts of pages could be refreshed via AJAX so these events will not fire.

    4. Disabling internet printing via the registry. This pops up a dialog saying that the action can not be taken. This popup poses the same problem as popping up the print dialog.

    5. Disabling javascript is not an option as some pages use AJAX and require the javascript to work properly.


    What remains to be tried (but I don't really know how):
    1. Using a hook to catch the print dialog and cancel/close it.

    2. Use a timer to look for the print dialog, via api, every XX milliseconds and close it when it is found.

    3. ???


    I don't know how to use the API in C# (although I am sure I could find plenty of documentation on it), so I have not tried anything with it yet.

    Any suggestions?

    Thanks in advance.
    Last edited by finn0013; Aug 11th, 2006 at 03:30 PM.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Disable Print in WebBrowser control

    if your application simply scrapes data, then why use the webbrowser control at all? why not use the WebClient object and just download the HTML source as a string and scrape the data you need?

    Since doing this isn't actually rendering any of the HTML page, it won't execute any javascript, and therefor won't bring up a print dialog.

    Or is there a reason why you need to do it in a browser control?

  3. #3

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    Re: Disable Print in WebBrowser control

    Yes, which is also the reason why I can't disable javascript. Some of the pages will execute portions as AJAX calls. I don't really want to deal with executing all the scripts on my own, so it is easer to let the browser render then scrape the contents.

  4. #4

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    Re: Disable Print in WebBrowser control

    I am not really very picky about how the javascript print() call gets cancelled as long as it does not affect the AJAX and rendering of the rest of the page.

    I am not opposed to "listening" to the executing javascript and just parsing out the print() commands, if anyone knows how to do that...

  5. #5

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    Re: Disable Print in WebBrowser control

    I have figured part of it out.

    By taking advantage of the way javascript treats methods and allows for method assignment, I can use this code to override the print function and make it do nothing:

    Code:
    <script language="JavaScript">
    window.print=function emptyMethod() {};
    </script>
    This will change the pointer for the window.print() function to point at the emptyMethod() function, so when window.print() is called, nothing happens.

    The problem I have now is how to get it this script code into the document before the document displays. I have tried modifying the DocumentText attribute of in the DocumentCompleted, Navigated, Navigating, Validated, and VisibleChanged events, all with no luck. With each of these, it seems to load the first page fully, then process my addition as a second page.

    Any ideas how I can insert some script/html into the top of the document before it is rendered for the first time?

  6. #6

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    Re: Disable Print in WebBrowser control

    Since nobody has posted a way to modify the contents before the first display, I am guessing that the control will not let you do it?

    Does anyone know if there is a way to add a hook to the control to intercept the message to display the contents?

  7. #7

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    Re: Disable Print in WebBrowser control

    This has been resolved in this post:

    http://www.vbforums.com/showthread.php?t=421686

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width