Printing background image with TWebBrowser

Hi to everybody,
I have the following code for set the background image and then print the document.
The background image change but in print preview (and print also) I don't  never see the background image.
Somebody can help me?
Thanks
Massimo

procedure TForm1.PrintDocClick(Sender: TObject);
var
   backFileName, Ole1: OleVariant;

begin

   backFileName := currPath + 'backimage.bmp';
   WebBrowser.OleObject.document.body.background := backFileName;
   WebBrowser.ControlInterface.ExecWB(OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_PROMPTUSER, Ole1, Ole1);

end;
mvillaltaAsked:
Who is Participating?
 
Russell LibbyConnect With a Mentor Software Engineer, Advisory Commented:
This can be done, but you need to be aware of a few things...

1. This option, as well as the other settings are global (for IE usage) for the user. You can modify this setting, but doing so will affect IE as well as your app.
2. The setting can be changed directly by updating the registry key:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main
Print_Background = "yes"

The catch is that if the browser control is placed on the form and auto created during your form's creation process, it will have already read the settings in, and there is no documented way to get the browser to re-read these settings from the registry. This means that one of two things must be done.

- You must update the setting during the application's initialization process.
or
- You must free and recreate the browser control after the setting has been updated.

If you need the ability to toggle this feature, then you must use the second option (free/recreate). Otherwise, the following example code can be used, as it persists the original setting, updates to the desired setting, and resets the old setting back when the application closes out.

Regards,
Russell


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw, Registry;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1:               TForm1;

implementation
{$R *.DFM}

var
  regIE:               TRegistry;
  OldPrintBackground:  String;

procedure TForm1.Button1Click(Sender: TObject);
begin

  WebBrowser1.Navigate('http://www.delphipages.com/');

end;

procedure TForm1.Button2Click(Sender: TObject);
begin

  // Your code for setting the background image
  // ...
  //

  WebBrowser1.ControlInterface.ExecWB(OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_PROMPTUSER, EmptyParam, EmptyParam);

end;

initialization

  // Get current setting and updated with desired setting
  regIE:=TRegistry.Create;
  try
     regIE.RootKey:=HKEY_CURRENT_USER;
     regIE.OpenKey('Software\Microsoft\Internet Explorer\Main', True);
     if regIE.ValueExists('Print_Background') then
        OldPrintBackground:=regIE.ReadString('Print_Background')
     else
        OldPrintBackground:='no';
     regIE.WriteString('Print_Background', 'yes');
  finally
     regIE.Free;
  end;

finalization

  // Restore the old setting
  regIE:=TRegistry.Create;
  try
     regIE.RootKey:=HKEY_CURRENT_USER;
     regIE.OpenKey('Software\Microsoft\Internet Explorer\Main', True);
     regIE.WriteString('Print_Background', OldPrintBackground);
  finally
     regIE.Free;
  end;


end.



0
 
Russell LibbySoftware Engineer, Advisory Commented:
Massimo, this "feature" is controlled via IE options:

In Internet Explorer:
Tools | Internet Options | Advanced Tab | Printing | Print background colors and images

It usually defaults to off, which is why it does not display in preview/print. Run IE, turn this feature on, then try running your app again.

Regards,
Russell
0
 
mvillaltaAuthor Commented:
Ok,
but I don't force my user to set this every time he want to print, how I can make this automatically via application?
0
 
mvillaltaAuthor Commented:
GREAT!
Thanks Russel, you give me a perfect method for add this feature to my application.

Best regards
Massimo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.