A requirement of a program I am making is that it can 'print' a given URL into a PDF - the server has a PDF printer installed (and is the default printer), so it's just a matter of printing the page programmaticly, and it seems the best way is to use the WebBrowser control - however, I am running into issues

protected void Page_Load(object sender, EventArgs e)

        {

            Thread thread = new Thread(new ThreadStart(Print));

            thread.SetApartmentState(ApartmentState.STA);

            thread.Start();

            thread.Join();

        }



        public void Print()

        {

            WebBrowser wb = new WebBrowser();

            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);

            wb.Url = new Uri("http://www.google.co.uk");

            wb.Navigate("http://www.google.co.uk");

            

            

            while (wb.ReadyState != WebBrowserReadyState.Complete)

            {

                System.Windows.Forms.Application.DoEvents();

            }

        }



        void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

        {

            WebBrowser wb = (WebBrowser)sender;

            wb.Print();



        }

With the above code, I get a script error 'dialogArguments.___IE_PrintType' is null or not an object' in shdoclc.dll/preview.dlg. If I have wb.Print() to somethign like wb.ShowSaveDialog() (or something like that, can't remember exactly), I notice the 'wb' object's document is a blank page, with just <html> tags and named 'about:blank'. But strangely, almost identical code (without the threading bit in the page_load method) works perfect in a normal Winforms based application.

Any ideas folks? Thanks!

Any ideas?

Your Webrowser is null because it is being called as a new webbrowser put your initial webbrowser outside of initial void like this:

WebBrowser wb = new WebBrowser();
public void Form1_Load(object sender, EventArgs e)
{
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
}

 wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

        {
// web browser is being recalled just use wb.Print();
            //WebBrowser wb = (WebBrowser)sender;

            wb.Print();



        }

Edited 3 Years Ago by Nick Evan: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.