• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2448
  • Last Modified:

TWebBrowser and Print - Page Setup

Could someone please tell me how to set the page settings - Header, Footer, Orientation and margins when I'm printing from TWebBrowser.
I'm printing using .. ControlInterface.ExecWB, and I beleive I've seen this used for PageSetup also.

DR
0
DelRunner
Asked:
DelRunner
  • 2
  • 2
1 Solution
 
ginsonicCommented:
listening
0
 
intheCommented:
hi,
you can using embeddedwb as its interface is setup to do it,i never seen code for normal twebbrowser to do it.:
http://www.euromind.com/IEDELPHI/embeddedwb.htm
(way superior version of TWebbrowser)

below is taken from tips /faq on embededwb:
Regards Barry

4. Set Print-Options programmatically. (Version 1)

The following sample shows an easy way to programmatically set options for print (number of copies, paper orientation, header, footer, margins etc.). The program opens the Pagesetup and Print dialogs, makes the modifications and close them again before they become visible. You can use the functions EnumChildWindows and GetCtrlId to get the ControlID for other options you want to set programmatically.

 

Add to private section in your main form:
procedure WMActivate(var Msg: TWMActivate); message WM_ACTIVATE;


type

TPrintOptions = record
  Copies: Integer;    //Number of copies
  Portrait: Boolean;  //Paper orientation
  Left: string;         //Left Margin
  Top: string;         //Top Margin
  Right: string;       //Right Margin
  Bottom: string;    //Bottom Margin
  Header: string;    //Header string
  Footer: string;    //Footer string
end;

var
PrintOptions: TPrintOptions;
PrintWithOptions: Boolean;

procedure TForm1.WMActivate(var Msg: TWMActivate);
var
  S: string;
  h, wnd: HWND;
  Item, I: Integer;
begin
if PrintWithOptions and (msg.active = 0) then begin
  PrintWithOptions := false;
  wnd := Msg.ActiveWindow;
  I := GetWindowTextLength(wnd);
  SetLength(S, I + 1);
  GetWindowText(Wnd, PChar(S), I + 1);
  if S = 'Print'#0 then begin
     SetDlgItemInt(wnd, 1154, PrintOptions.Copies, FALSE);
    SendDlgItemMessage(Wnd, 1, BM_CLICK, 0, 0);
    PrintWithOptions := true;
    end else
  if S = 'Page Setup'#0 then begin
    if PrintOptions.Portrait then
    SendDlgItemMessage(Wnd, 1056, BM_CLICK, 0, 0)
    else
    SendDlgItemMessage(Wnd, 1057, BM_CLICK, 0, 0);
    SetDlgItemText(wnd, 8147, PChar(PrintOptions.Header));
    SetDlgItemText(wnd, 8149, PChar(PrintOptions.Footer));
    SetDlgItemText(wnd, 1155, PChar(PrintOptions.Left));
    SetDlgItemText(wnd, 1156, PChar(PrintOptions.Top));
    SetDlgItemText(wnd, 1157, PChar(PrintOptions.Right));
    SetDlgItemText(wnd, 1158, PChar(PrintOptions.Bottom));
    SendDlgItemMessage(Wnd, 1, BM_CLICK, 0, 0);
    PrintWithOptions := true;
  end;
end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
if Assigned(EmbeddedWB1.Document) then begin
  with PrintOptions do begin
     Copies := 2;
    Portrait := FALSE;
    Footer := 'This is my Footer';
    Header := 'This is my Header';
    Left := '25,0';
    Top := '20,0';
    Right := '22,0';
    Bottom := '19,7';
  end;
PrintWithOptions := True;
Embeddedwb1.pagesetup;
embeddedwb1.printsetup;
PrintWithOptions := False;
end else showmessage('No Document to print!');
end;


 

 

5. Set Print-Options programmatically. (Version 2)



You can specify extended printing information by passing in the SAFEARRAY structure through the VARIANT argument vaIn, when you use ExecWB and OLECMDID_PRINT. This SAFEARRAY data type takes a maximum of five items:
A string (BSTR) that contains a custom header.

A string (BSTR) that contains a custom footer.

An IStream object that contains an HTML file that serves as an "optional header." This is the e-mail header that you see in Microsoft Outlook and Microsoft Outlook Express e-mail messages when you print them. This IStream must point to a full, valid HTML document, not HTML fragments, or it will print incorrectly.

An alternative URL to use for the document. This is only relevant to Outlook and Outlook Express.

A set of printing flags (dwFlags) to configure the printer. This is only relevant to Outlook and Outlook Express.

All versions of Internet Explorer, from version 4.0 onward, support the use of the IWebBrowser2::ExecWB method to customize headers and footers:

procedure TForm1.PrintHeaderFooter(Header, Footer, HTMLHeader: string);
var
  CmdTarget: IOleCommandTarget;
  Stream: IStream;
  v, v1: OleVariant;
  Dummy: Int64;
  Psa: PSafeArray;
begin
  CreateStreamOnHGlobal(0, TRUE, Stream);
  Stream.Write(Pchar(HTMLHeader), length(HTMLHeader), @Dummy);
  Stream.Seek(0, STREAM_SEEK_SET, Dummy);
  SafeArrayCopy(PSafeArray(TVarData(VarArrayOf([Header, Footer, Stream as IUnknown])).VArray), psa);
  TVarData(V1).VType := varArray or varByRef;
  SafeArrayCopy(psa, PSafeArray(TVarData(V1).VArray));
  if Webbrowser1.Document <> nil then
  try
    Webbrowser1.Document.QueryInterface(IOleCommandTarget, CmdTarget);
    if CmdTarget <> nil then
    try
      CmdTarget.Exec(nil, OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, v1, v);
    finally
      CmdTarget := nil;
    end;
  except end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  HTMLHeader: string;
begin
  HTMLHeader := '<HTML><BODY><FONT SIZE=4><B><CENTER>IE & DELPHI</CENTER></B></FONT><HR></BODY></HTML>';
  PrintHeaderFooter('My Header', 'My Footer', HTMLHeader);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  webbrowser1.navigate('about:blank');
end;

     
 

More Info: HOWTO: Print Custom Headers and Footers for a WebBrowser Control
http://support.microsoft.com/support/kb/articles/Q267/2/40.ASP
0
 
DelRunnerAuthor Commented:
I've tried the suggestions, but without luck.
I'm getting errors when I try to compile the embeddedwb, complaints in the MSHTML_tlb.
I was hoping more on using the ExecWB.
Here I'm getting errors on the variable definitions:
IStreab, PSafeArray..

Any suggestions ?
I increase a qualified answer with further 100 p.

DR
0
 
DelRunnerAuthor Commented:
Hello.....!
Nearly there. I've got the EmbeddedWB control working, but I forgot to mention that this should be for both NT4 and Win2000.
Do you know if the code is compatible for both ?

DR.
0
 
intheCommented:
>>EmbeddedWB control working

glad to hear ,it will save you alot of headaches believe me :)

well i know it works fine on win2000 , i really cant honestly say for nt4 but i dont see any problems ,well i can say its more dependant on ie than OS.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now