开发者

Printing to a specific printer using the IE control without changing the default printer

Is there a way for me to pass a PrintDlg structure (or something similar) to my WebBrowser (Internet Explorer / IWebBrowser2) control such that it will print to the printer specified in the PrintDlg without popping up a print dialog? I want to do this without changing the default printer. IWebBrowser2::ExecWB provides a way to skip the dialog pop-up (OLECMDEXECOPT_DONTPROMPTUSER), but not a way to specify a printer.

There are two problems with just changing the default printer: 1) it's slow, 2) Internet Explorer keeps its current printer cached. When you print to the default printer, it will cache that printer. If you then change the default printer and tell it to print again, IE will not use the new default printer, it will used its cached current printer.

After IE 5.5, the current printer is not stored in the Windows registry.

As an alternative, I researched 4 different ideas for getting the image from the IE control and printing myself rather than getting the control to print itself:

  1. Using IViewObject::Draw. I can get the image, but can't get it to scale to paper the same way that it does when printed for IE:

    RECTL rect;

    rect.left = 0;

    rect.top = 0;

    rect.right = GetDeviceCaps(printerhdc, PHYSICALWIDTH);

    rect.bottom = GetDeviceCaps(printerhd开发者_StackOverflow中文版c, PHYSICALHEIGHT);

    StartDoc(printerhdc, &di);

    StartPage(printerhdc);

    HRESULT hr = pViewObj->Draw(DVASPECT_DOCPRINT, page, NULL, pTargetDevice, printerhdc, printerhdc, &rect, NULL, NULL, 0); //prints too small

    EndPage(printerhdc);

    EndDoc(printerhdc);

  2. Sending a WM_PAINT to the IE control. I haven't done much with this one, as I read in a thread online that this didn't work that well.

  3. AxDHTMLEDLib. Very little documentation, and I think this is just for .net

  4. Using IHTMLElement, IHTMLElementRender->SetDocumentPrinter, IHTMLElementRender->DrawToDc. Microsoft has withdrawn support with IE9

Any ideas you may have would be wildly appreciated! Thanks in advance!


If you have some flexibility of using a default printer, here is another idea. It is a bit round-a-about though.

  • make your default printer some PDF writer (like Amyuni PDF or Adobe Distiller). These printer drivers convert the incoming job into a PDF file and you can set it up to save the file in some disk location.

  • use a PowerShell to print this PDF file to your target printer you need. You can either use the PrintUI DLL interface or use and the "print" verb. http://www.eggheadcafe.com/software/aspnet/30441939/how-to-suppress-printdialog-when-using-print-verb.aspx provides an example. By doing this, you can do all your printer queue customization in this script and leave the default printer as-is

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜