I am new in c#, I'm doing a printing application, currently I'm using WebBrowser to print out the HTML file, can I programmatically change the printer setting rather than manually change it through print dialog. I want select the printer tray for printing different HTML file, hope someone can provide solution for me. Thanks!
2 Answers
A much used method is changing the default printer prior to printer, but personally I've had too many problems with that. (As in the printer isn't changed back correctly afterwards, or a user print task is started simultaneously).
You can do a lot more with Print Templates. However they are also a lot more work (but worth it imo). Besides setting the printer, you can set header/footer/margin/papertype/etc. Can't post an example project here, but using a template based on the example in the link above, the printer can be set with (template is in javascript)
var PrinterName = "{Printer}";
if (PrinterName.length > 0){
dialogArguments.__IE_PrinterCMD_Printer = PrinterName;
}
The template is written with the, well uhm, printertemplate-template, and {Printer} is replaced with the PrinterName on time of writing.
Then some interfacing comes into play, to call the webbrowsers underlying print command. (via exec ) Pseudo code:
const int print = 6;
string templatelocation = "FileName";
((IOleCommandTarget3)WebBrowser.Document.DomDocument).Exec(IntPtr.Zero, print, 0, ref templatelocation, ref templatelocation);
This is very limited info and it would take a lot more work still to get it working, but then you'd have all the options you would need regarding html printing. Thus far the 'simple' options. For setting the tray, you'd have to pass a devmode object to the printtemplate. I could muster up an example project, but don't know if I can get around to that today.