I'm trying to print multiple copies of an HTML document using the ICoreWebView2_16
's Print()
function. The documentation states that "Only one Printing operation can be in progress at a time.". I've tried using a for loop to print multiple times:
for I := 1 to NumberOfCopies do
begin
(EdgeBrowser.DefaultInterface as ICoreWebView2_16).Print(PrintSettings, CoreWebView2PrintCompletedHandler);
end;
This doesn't work because, as stated, only one print operation can happen at a time.
I've also tried using the PrintSettings
parameter to indicate the number of copies, but there are no members for setting the number of copies in the ICoreWebView2PrintSettings
interface. There is a member for settings the number of copies in the ICoreWebView2PrintSettings2
interface... But that can't be used as a parameter for the ICoreWebView2_16.Print
function.
This was an interesting challenge! I have put together a pure Delphi program that accomplishes this, using only pure Delphi (11.3) plus the WebView2_TLB.pas file generated by tlibimp.exe from a new enough version of WebView2 (1.0.1587.40 in my case).
The key insight is that if only the WebView2 installed is new enough, then functions that would seem to take an older version of an interface as parameter can often actually take also a newer version of that interface.
This is the case with the
ICoreWebView2_16.Print
function. Even though its declaration says that its parameter is "just" anICoreWebView2PrintSettings
, and not anICoreWebView2PrintSettings2
, you can still pass in anICoreWebView2PrintSettings2
-- if the installed version of WebView2 supports it.There is a good example by Microsoft at https://github.com/MicrosoftDocs/edge-developer/blob/main/microsoft-edge/webview2/how-to/print.md
I've adapted that to Delphi, and placed it on Github here - feel free to use that code and modify it further: https://github.com/bolligerm/delphi/tree/master/varia/GutenbergMini
I paste the core of the code here as well: