Printing HTML from Window Forms

G

Guest

What's the best way to print an html document generated in a Windows forms
application?

IE (WebBrowser control) works pretty well; with some effort to use a print
template, you can control margins, headers, footers, and get print preview.
The 'gotcha' is that (as best I can tell), there's no good way to specify the
printer that you want to print to. I tried calling the windows api to set
the default printer, and this works sometimes, but my application needs to
send different print jobs to different printers, and the selections sometimes
'step on' each other when you switch the default printer.

I also looked at the ReportViewer, but I could not find a way to get it to
render html.

Am I missing something? Surely there's a good way to print html from
Windows forms.
 
L

Luke Zhang [MSFT]

Hi Taylor,

Thanks for posting.

As I understood, the problem is on the switch default printer method. Here
is a sample code for changing the default printer, maybe u can use it in
your project:


private ManagementObjectSearcher query;

private ManagementObjectCollection queryCollection;

string _classname="SELECT * FROM Win32_Printer";

query=new ManagementObjectSearcher(_classname);


queryCollection = query.Get();



foreach (ManagementObject mo in queryCollection)

{

if
(string.Compare(mo["Name"].ToString(),"printer name you want",true)==0)

{


mo.InvokeMethod("SetDefaultPrinter",null);

break;

}

}



Besides, I am not sure if it is project or machine specific. I recommended
you to perform the tests below:



1. Create a new project, then perform the same steps lead the problem, does
the problem exist?

2. If the problem existed in the new created project, we may try above test
on another machine, does the problem still exist?



With the above 2 steps, we can determine if the problem can be reproduced
out independent of project or machine.



If you can reproduce this problem, can you provide the details steps to
help us reproduce the problem? Then we can understand the problem much
better.



I look forward to hearing from you.

Thanks





Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Luke,

Thanks for the suggestion, but I don't have any trouble changing the default
printer. The problem is that this is not a good solution. In general,
changing the user's selection seems like a bad thing to do, and in my
particular situation, I need to print different documents to different
printers, sometimes at times very close to each other. The way I'm doing it
now, it ends up sending documents to the wrong printer quite often. I could
add another workaround to queue my print jobs, but this seems like a bad
patch to a bad design.

So, my main question is: Is there a better overall approach for printing HTML?

Of course, if there is a way to tell the WebBrowser control to print using a
specific printer (without changing the default), I would also be interested
in that.

Thanks,
Thomas
 
L

Luke Zhang [MSFT]

I think WebBrower control is the most convenient way to print a HTML
document. So, the problem is, sending documents to the wrong printer.
WebBrower control will print to the default printer, if we set the default
printer correctly, and then we can fix the problem, isn't it? Have you
tried the code in my previous message to set the default printer? Can it
help on the problem "ending documents to the wrong printer"?

Regards,

Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Perhaps it's the most convenient way, but it's not a reliable one; at least
if you need to print to something other than the default printer.

I did not use your code, but as I said before, I have no problem changing
the default printer. I just used the win api:

[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetDefaultPrinter(string name);

However, this is a bad solution because (in my opinion), it is not good to
change a user's default, even if you change it back after printing. Also,
this does not reliably change the printer that IE uses. It works just fine
on my development machine (Win 2003), but on a couple of test machines (Win
XP/IE 6), IE still prints to the previous default printer. I tried the
following to let IE know that the printer changed, but this does not work (at
least on some machines).

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg,
UIntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags fuFlags,
uint uTimeout, int lpdwResult);

SendMessageTimeout((IntPtr)HWND_BROADCAST,
WM_SETTINGCHANGE,
(UIntPtr)0, (IntPtr)0,
SendMessageTimeoutFlags.SMTO_NORMAL,
1000,
result);

Even if this worked, it seems like a bad hack (changing the default printer).

My conclusion after many hours at this problem is that IE (WebBrowser
control) is not up to the task of printing reliably from a Windows Forms. I
would be a 'happy camper' if someone could prove me wrong.

Maybe you could suggest to the team in charge of WebBrowser that they should
add a bit more functionality to the managed control so that you can specify a
printer. Ability to define headers, footers, and margins from managed code
(and without resorting to a print template) would also be a great addition.

Thanks for the response.

Thomas
 
L

Luke Zhang [MSFT]

The WebBrowser in .NET 2005 is a managed wrapper for the WebBrowser
ActiveX control, it cannot work with the PrintDocument object in .NET,
which can be print to different printer. Hope this can be improve in
furture. Currently, we can only to change the default printer as a work
around.

Regards,

Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Similar Threads


Top