Use PrintDialog to print out formatted HTML

  • Thread starter Thread starter Artie
  • Start date Start date
A

Artie

Hi,

I've searched the web but can't find a solution to an apparently
really simple problem.

My app contains an HTML string and I need to be able to invoke the
Print Dialog to print the HTML correctly formatted (i.e. not as raw
HTML) to a printer that the User chooses.

So, I need something like:

string _html = "<html> some html stuff </html>";

PrintDialog printDialog = new PrintDialog( );
if ( PrintDialog =OK )
{
printer.Print( _html );
}

I want this to invoked the common Windows PrintDialog, and once the
user selects their options and clicks OK, the formatted HTML is
printed.

Any help much appreciated

Cheers

Artie
 
Artie,

I would use COM interop and access the MSHTML.Document class. You would
cast that to IHTMLDocument2, and then call the execCommand method on that
interface, passing the Print command identifier. I'm recommending
MSHTML.Document because you don't want to have to render the document
visually.

Calling that method will bring up the print dialog, and then print your
HTML.

It should be said that you will have to load the HTML into the
MSHTML.Document instance, which isn't too bad. You can just save the HTML
to a file temporarily and then call navigate to navigate to that file.
 
Hi ,

Your problem is that you need the HTML parsed, an option could be use a
WebBrowser control and set Visible to false, then use the Print() method.
 
Hi ,

Your problem is that you need the HTML parsed, an option could be use a
WebBrowser control and set Visible to false, then use the Print() method.

--
Ignacio Machinhttp://www.laceupsolutions.com












- Show quoted text -

Thanks very much for your reply Ignacio.

I should maybe have given a bit more information in my original post.
I actually DO already have a WebBrowser control visible on my form, so
I'll look into the Print() command.

I also have a similar problem with saving the formatted HTML from this
control, which I've posted in the topic "Invoke 'Save As' Dialog to
save WebBrowser control contents".

If you could give me some pointers on this one too, I'll be extremely
grateful.

Cheers

Artie
 
Hi ,

Your problem is that you need the HTML parsed, an option could be use a
WebBrowser control and set Visible to false, then use the Print() method.

--
Ignacio Machinhttp://www.laceupsolutions.com












- Show quoted text -

This actually ended up being a lot easier than I'd expected.

After your note about using a WebBrowser control (which we already had
anyway), it appears the following does pretty much most of what we
need:

webBrowser.Document.ExecCommand("Print", true, 0);

The only thing it doesn't seem to offer is a return code if the User
clicked Cancel rather than printing.

Is there a straightforward way of detecting that?

Artie
 
Hi,


Good to know you were able to solve it. I do not know of any way to get the
return code of the print operation(do you really care abut it?)
Regarding to save it as rendered (your other post) I do not know of any
method for doing that. not only that, but in what format you want to save it
into? (word, pdf, etc) ?
A possible option is saving it in .xps format, but you need office 07 for
that.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
Hi ,

Your problem is that you need the HTML parsed, an option could be use a
WebBrowser control and set Visible to false, then use the Print() method.

--
Ignacio Machinhttp://www.laceupsolutions.com
message












- Show quoted text -

This actually ended up being a lot easier than I'd expected.

After your note about using a WebBrowser control (which we already had
anyway), it appears the following does pretty much most of what we
need:

webBrowser.Document.ExecCommand("Print", true, 0);

The only thing it doesn't seem to offer is a return code if the User
clicked Cancel rather than printing.

Is there a straightforward way of detecting that?

Artie
 
Back
Top