1

The TWebBrowser component has been replaced by the TEdgeBrowser. My goal is to customize the text in the header and footer sections of a printed HTML page when the user prints using the TEdgeBrowser component.

I was able to accomplish this when using the TWebBrowser component by writing code to temporarily set the Internet Explorer registry keys at the time of printing ("SOFTWARE\Microsoft\Internet Explorer\PageSetup": 'header' and 'footer' keys). However, these same keys don't seem to exist for the Edge browser. The closest I found was the 'PrintHeaderFooter' key, which enables/disables the headers and footers.

Is there a way to modify the header and footer content in my Delphi application when printing using the new TEdgeBrowser component?

3

1 Answer 1

2

When printing yourself using the Print method of the WebView2 control, you can specify your own strings to use instead of document title and document uri in the header and footer:

procedure TForm1.Button1Click(Sender: TObject);
var
  PrintSettings: ICoreWebView2PrintSettings;
begin
  (EdgeBrowser1.EnvironmentInterface as ICoreWebView2Environment6).CreatePrintSettings(PrintSettings);

  PrintSettings.Set_ShouldPrintHeaderAndFooter(true.ToInteger);
  PrintSettings.Set_HeaderTitle('New Header text');
  PrintSettings.Set_FooterUri('New Footer text');

  (EdgeBrowser1.DefaultInterface as ICoreWebView2_16).Print(PrintSettings, nil);
end;

You can also control more stuff via PrintSettings like selecting the printer to use by name, choosing paper size and page orientation, etc.


In order to get access to the types ICoreWebView2PrintSettings, ICoreWebView2Environment6 and ICoreWebView2_16 follow the steps in Marcodor's answer at WebView2 (TEdgeBrowser) updated Delphi interface (e.g. ICoreWebView2Controller2).


3
  • Thanks. I've attempted to implement your solution. The headers and footers are shown as expected, but the content of the print appears blank even though there is content loaded in my EdgeBrowser. Sep 28 at 14:19
  • Hm, strange, it works for me. I have tested with several different websites. Can you share the website you use for testing or a html file that reproduces the issue?
    – NineBerry
    Sep 28 at 14:23
  • Figured it out. It was an issue unrelated to your code. @NineBerry Thanks. Oct 4 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.