471,350 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Best way to print a HTML file?

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.
Nov 29 '05 #1
1 4525
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

"David Templet" <jd*******@gmail.com> wrote in message
news:DB**********************************@microsof t.com...
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.

Nov 29 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by James | last post: by
4 posts views Thread by Frank Millman | last post: by
7 posts views Thread by Rhino | last post: by

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.