Best way to print a HTML file?

G

Guest

I have a C# application that generates its reports in HTML. After it creates
the report, it opens it in IE so it can be viewed or printed.

I would like to add the ability to print the report directly from the
application, but I haven't found an easy way to do this. I am probably making
it overly complicated, but this is what I am doing right now:

1. Instance an "AxWebBrowser" control
2. Navigate the browser control to the report HTML file
3. Sleep until the browser control is ready (finished rendering the HTML file)
4. Cast the browser’s Document to an IHTMLDocument2 class
5. Call .execCommands(“Printâ€,true,0) and tell the IHTMLDocument2 to print

This works fine, but it just seems like there should be a better way. I
would also like to have control over the document's header/footer, and have
the ability just to print directly to the default printer and bypass the
printer selection dialog box (if the user wanted to do it this way).

Does anyone happen to know of a better way to print HTML documents in C#?
Thanks in advance.
 
K

Kevin Spencer

Not sure what you mean by "better." But it looks like you've got the right
idea to me. An HTML document is plain text. It has to be interpreted to be
formatted, and have resources such as images downloaded and formatted in
line with the rest of the document. After that, to print, the final graphic
product must be sent to the printer as a graphic. So, you could always write
your own parser for HTML, have it build a graphic based on the markup, in
memory, and send that to a printer, but unless you are planning on marketing
a browser (essentially what you would build), you may as well use an
existing one.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top