Mithun
Mithun

Reputation: 196

How to print HTML document along with CSS style programmatically using C#.NET?

I'm doing a C# project that should have a feature of printing HTML pages. I used WebBrowser control's Print() function. Confusingly, the function strips CSS styles from the HTML page while printing! Can you please tell me an alternative solution that prints HTML page along with CSS styles?

Upvotes: 0

Views: 2150

Answers (4)

Mithun
Mithun

Reputation: 196

As none of the answers helped me, I tried hard finding out the actual problem. Now, I can figure out what actually is wrong.

The problem is with my Internet Explorer's page setting! Don't get it? Okay, let me explain it. Microsoft .NET's WebBrowser class just wraps the unmanaged Internet Explorer's functionality to form a managed version. So, if anything was wrong in IE, it would also affect the WebBrowser class.

enter image description here

In my case, my Internet Explorer's 'page setting' was not configured to print out background colors and images. All I gotta do is to just click the check box which is labelled as 'Print Background Colors and Images'. That's it! :) Now, my application happily prints out the HTML page with CSS styles!

Anyways, I thank all the three guys who tried to help me.

Upvotes: 4

WraithNath
WraithNath

Reputation: 18013

Have you tried adding:

 @media screen, print {
   //css goes here
  }

to your css file? this will specify the css is used for on screen and printing

Upvotes: 0

wegelagerer
wegelagerer

Reputation: 3720

You'll have to create different css for printing which will contain media="print" tag instead of media="screen".

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174457

Is the CSS applied, when you view the website in the WebBrowser?
It might be, that the site simply has a different CSS style for printing?
In general, the WebBrowser should print with CSS, because its just the IE encapsulated.

Upvotes: 0

Related Questions