Search code examples
c#wpfwebbrowser-controlprint-preview

WebBrowser Print Preview Command


I'm just trying to get a print preview of an html document contained within a Web Browser control. I'm successfully able to print the document being displayed using this code:

IHTMLDocument2 doc = WebBrowser.Document as IHTMLDocument2; doc.execCommand("Print", false, null);

I realize that there are other methods to print the document such as creating an xps document and then handing it the paginator, however this solution just seems very nice.

Now I'm just wondering why there isn't a similar command to do the same for displaying a print preview. I've looked at the list of command identifiers: https://msdn.microsoft.com/en-us/library/ms533049(v=vs.85).aspx and didn't manage to find a print preview command... or a print command haha, which makes me think I'm looking in the wrong place, although that list seemed to correspond with the object I'm using as those are the command identifiers for the execCommand function.

The top answer to this question: Displaying Print Preview of HTML Document without DocumentViewer worked for me and I was successfully able to display a print preview. However, the code is confusing and seems bloated.

Is there a way to display a print preview using the above method? Also, where is the actual list of commands for the execCommand function?

Thanks.


Solution

  • The complete list of command identifiers that's supported is documented here on MSDN.

    The bit that's poorly documented is this is the list of command identifiers, not strings that execCommand uses. You're calling IDM_EXECPRINT, which is aliased as a string simply as "Print". Most of the other strings are mapped to the command identifier just without the "IDM_" portion of the identifier.

    As you can see from that list, there's no version of a print preview, so I'm afraid you'll need to find another solution.